Răsfoiți Sursa

Minor endianess

Marcin Krzyzanowski 7 ani în urmă
părinte
comite
c34003def3

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

@@ -30,9 +30,9 @@ extension Int {
 extension FixedWidthInteger {
     @_transparent
     func bytes(totalBytes: Int = MemoryLayout<Self>.size) -> Array<UInt8> {
-        return arrayOfBytes(value: self, length: totalBytes)
+        return arrayOfBytes(value: self.littleEndian, length: totalBytes)
         // TODO: adjust bytes order
-        // var value = self
+        // var value = self.littleEndian
         // return withUnsafeBytes(of: &value, Array.init).reversed()
     }
 }

+ 5 - 5
Sources/CryptoSwift/SHA1.swift

@@ -133,11 +133,11 @@ extension SHA1: Updatable {
         var result = Array<UInt8>(repeating: 0, count: SHA1.digestLength)
         var pos = 0
         for idx in 0..<accumulatedHash.count {
-            let h = accumulatedHash[idx].bigEndian
-            result[pos] = UInt8(h & 0xff)
-            result[pos + 1] = UInt8((h >> 8) & 0xff)
-            result[pos + 2] = UInt8((h >> 16) & 0xff)
-            result[pos + 3] = UInt8((h >> 24) & 0xff)
+            let h = accumulatedHash[idx]
+            result[pos + 0] = UInt8((h >> 24) & 0xff)
+            result[pos + 1] = UInt8((h >> 16) & 0xff)
+            result[pos + 2] = UInt8((h >> 8) & 0xff)
+            result[pos + 3] = UInt8(h & 0xff)
             pos += 4
         }
 

+ 14 - 14
Sources/CryptoSwift/SHA2.swift

@@ -315,25 +315,25 @@ extension SHA2: Updatable {
         case .sha224, .sha256:
             var pos = 0
             for idx in 0..<accumulatedHash32.count where idx < variant.finalLength {
-                let h = accumulatedHash32[idx].bigEndian
-                result[pos] = UInt8(h & 0xff)
-                result[pos + 1] = UInt8((h >> 8) & 0xff)
-                result[pos + 2] = UInt8((h >> 16) & 0xff)
-                result[pos + 3] = UInt8((h >> 24) & 0xff)
+                let h = accumulatedHash32[idx]
+                result[pos + 0] = UInt8((h >> 24) & 0xff)
+                result[pos + 1] = UInt8((h >> 16) & 0xff)
+                result[pos + 2] = UInt8((h >> 8) & 0xff)
+                result[pos + 3] = UInt8(h & 0xff)
                 pos += 4
             }
         case .sha384, .sha512:
             var pos = 0
             for idx in 0..<accumulatedHash64.count where idx < variant.finalLength {
-                let h = accumulatedHash64[idx].bigEndian
-                result[pos] = UInt8(h & 0xff)
-                result[pos + 1] = UInt8((h >> 8) & 0xff)
-                result[pos + 2] = UInt8((h >> 16) & 0xff)
-                result[pos + 3] = UInt8((h >> 24) & 0xff)
-                result[pos + 4] = UInt8((h >> 32) & 0xff)
-                result[pos + 5] = UInt8((h >> 40) & 0xff)
-                result[pos + 6] = UInt8((h >> 48) & 0xff)
-                result[pos + 7] = UInt8((h >> 56) & 0xff)
+                let h = accumulatedHash64[idx]
+                result[pos + 0] = UInt8((h >> 56) & 0xff)
+                result[pos + 1] = UInt8((h >> 48) & 0xff)
+                result[pos + 2] = UInt8((h >> 40) & 0xff)
+                result[pos + 3] = UInt8((h >> 32) & 0xff)
+                result[pos + 4] = UInt8((h >> 24) & 0xff)
+                result[pos + 5] = UInt8((h >> 16) & 0xff)
+                result[pos + 6] = UInt8((h >> 8) & 0xff)
+                result[pos + 7] = UInt8(h & 0xff)
                 pos += 8
             }
         }