Marcin Krzyżanowski 7 жил өмнө
parent
commit
577e94f606

+ 1 - 1
Sources/CryptoSwift/ChaCha20.swift

@@ -223,7 +223,7 @@ public final class ChaCha20: BlockCipher {
             var u: UInt32 = 1
             for i in 0..<4 {
                 u += UInt32(counter[i])
-                counter[i] = UInt8(u)
+                counter[i] = UInt8(u & 0xFF)
                 u >>= 8
             }
             bytes = Array(bytes[ChaCha20.blockSize..<bytes.endIndex])

+ 4 - 0
Sources/CryptoSwift/Foundation/Data+Extension.swift

@@ -80,6 +80,10 @@ extension Data {
 
 extension Data {
 
+    public init(hex: String) {
+        self.init(bytes: Array<UInt8>(hex: hex))
+    }
+
     public var bytes: Array<UInt8> {
         return Array(self)
     }

+ 6 - 6
Sources/CryptoSwift/Int+Extension.swift

@@ -23,17 +23,17 @@
 
 /* array of bits */
 extension Int {
-
     init(bits: [Bit]) {
         self.init(bitPattern: integerFrom(bits) as UInt)
     }
 }
 
-/* array of bytes */
-extension Int {
-
-    /** Array of bytes with optional padding */
-    func bytes(totalBytes: Int = MemoryLayout<Int>.size) -> Array<UInt8> {
+extension FixedWidthInteger {
+    @_transparent
+    func bytes(totalBytes: Int = MemoryLayout<Self>.size) -> Array<UInt8> {
         return arrayOfBytes(value: self, length: totalBytes)
+        // TODO: adjust bytes order
+        // var value = self
+        // return withUnsafeBytes(of: &value, Array.init).reversed()
     }
 }

+ 0 - 4
Sources/CryptoSwift/UInt16+Extension.swift

@@ -36,8 +36,4 @@ extension UInt16 {
 
         self = val0 | val1
     }
-
-    func bytes(totalBytes: Int = MemoryLayout<UInt16>.size) -> Array<UInt8> {
-        return arrayOfBytes(value: self, length: totalBytes)
-    }
 }

+ 0 - 4
Sources/CryptoSwift/UInt32+Extension.swift

@@ -47,8 +47,4 @@ extension UInt32 {
 
         self = val0 | val1 | val2 | val3
     }
-
-    func bytes(totalBytes: Int = MemoryLayout<UInt32>.size) -> Array<UInt8> {
-        return arrayOfBytes(value: self, length: totalBytes)
-    }
 }

+ 0 - 4
Sources/CryptoSwift/UInt64+Extension.swift

@@ -42,8 +42,4 @@ extension UInt64 {
 
         self = val0 | val1 | val2 | val3 | val4 | val5 | val6 | val7
     }
-
-    func bytes(totalBytes: Int = MemoryLayout<UInt64>.size) -> Array<UInt8> {
-        return arrayOfBytes(value: self, length: totalBytes)
-    }
 }