소스 검색

arrayOfBytes length is optional. Simpler version of integerFromBitsArray

Marcin Krzyżanowski 11 년 전
부모
커밋
677cf997de
4개의 변경된 파일7개의 추가작업 그리고 6개의 파일을 삭제
  1. 4 3
      CryptoSwift/Generics.swift
  2. 1 1
      CryptoSwift/IntExtension.swift
  3. 1 1
      CryptoSwift/UInt32Extension.swift
  4. 1 1
      CryptoSwift/UInt64Extension.swift

+ 4 - 3
CryptoSwift/Generics.swift

@@ -22,12 +22,12 @@ extension UInt32:Initiable {}
 extension UInt64:Initiable {}
 
 /** build bit pattern from array of bits */
-func integerFromBitsArray<T: UnsignedIntegerType where T: IntegerLiteralConvertible, T: Initiable>(bits: [Bit]) -> T
+func integerFromBitsArray<T: UnsignedIntegerType>(bits: [Bit]) -> T
 {
     var bitPattern:T = 0
     for (idx,b) in enumerate(bits) {
         if (b == Bit.One) {
-            let bit = T(1 << idx)
+            let bit = T.from(UIntMax(1) << UIntMax(idx))
             bitPattern = bitPattern | bit
         }
     }
@@ -54,7 +54,8 @@ func integerWithBytes<T: IntegerType>(bytes: [Byte]) -> T {
 }
 
 /** array of bytes, little-endian representation */
-func arrayOfBytes<T>(value:T, totalBytes:Int) -> [Byte] {
+func arrayOfBytes<T>(value:T, length:Int? = nil) -> [Byte] {
+    let totalBytes = length ?? (sizeofValue(value) * 8)
     var v = value
     
     var valuePointer = UnsafeMutablePointer<T>.alloc(1)

+ 1 - 1
CryptoSwift/IntExtension.swift

@@ -27,7 +27,7 @@ extension Int {
 extension Int {
     /** Array of bytes with optional padding (little-endian) */
     public func bytes(_ totalBytes: Int = sizeof(Int)) -> [Byte] {
-        return arrayOfBytes(self, totalBytes)
+        return arrayOfBytes(self, length: totalBytes)
     }
 
     public static func withBytes(bytes: Slice<Byte>) -> Int {

+ 1 - 1
CryptoSwift/UInt32Extension.swift

@@ -11,7 +11,7 @@ import Foundation
 /** array of bytes */
 extension UInt32 {
     public func bytes(_ totalBytes: Int = sizeof(UInt32)) -> [Byte] {
-        return arrayOfBytes(self, totalBytes)
+        return arrayOfBytes(self, length: totalBytes)
     }
 
     public static func withBytes(bytes: Slice<Byte>) -> UInt32 {

+ 1 - 1
CryptoSwift/UInt64Extension.swift

@@ -11,7 +11,7 @@ import Foundation
 /** array of bytes */
 extension UInt64 {
     public func bytes(_ totalBytes: Int = sizeof(UInt64)) -> [Byte] {
-        return arrayOfBytes(self, totalBytes)
+        return arrayOfBytes(self, length: totalBytes)
     }
 
     public static func withBytes(bytes: Slice<Byte>) -> UInt64 {