Răsfoiți Sursa

Apply performance improvements to SHA2

Marcin Krzyżanowski 9 ani în urmă
părinte
comite
64a100f5ae
1 a modificat fișierele cu 4 adăugiri și 8 ștergeri
  1. 4 8
      Sources/CryptoSwift/SHA2.swift

+ 4 - 8
Sources/CryptoSwift/SHA2.swift

@@ -149,10 +149,8 @@ public final class SHA2: DigestType {
         for x in 0..<M.count {
         for x in 0..<M.count {
             switch (x) {
             switch (x) {
             case 0...15:
             case 0...15:
-                let start = chunk.startIndex.advanced(by: (x * MemoryLayout<UInt64>.size))
-                let end = start.advanced(by: MemoryLayout<UInt64>.size)
-                let le = chunk[start..<end].toUInt64Array()[0]
-                M[x] = le.bigEndian
+                let start = chunk.startIndex.advanced(by: x * 8) // * MemoryLayout<UInt64>.size
+                M[x] = UInt64(bytes: chunk, fromIndex: start)
                 break
                 break
             default:
             default:
                 let s0 = rotateRight(M[x-15], by: 1) ^ rotateRight(M[x-15], by: 8) ^ (M[x-15] >> 7)
                 let s0 = rotateRight(M[x-15], by: 1) ^ rotateRight(M[x-15], by: 8) ^ (M[x-15] >> 7)
@@ -208,10 +206,8 @@ public final class SHA2: DigestType {
         for x in 0..<M.count {
         for x in 0..<M.count {
             switch (x) {
             switch (x) {
             case 0...15:
             case 0...15:
-                let start = chunk.startIndex + (x * MemoryLayout<UInt32>.size)
-                let end = start + MemoryLayout<UInt32>.size
-                let le = chunk[start..<end].toUInt32Array()[0]
-                M[x] = le.bigEndian
+                let start = chunk.startIndex.advanced(by: x * 4) // * MemoryLayout<UInt32>.size
+                M[x] = UInt32(bytes: chunk, fromIndex: start)
                 break
                 break
             default:
             default:
                 let s0 = rotateRight(M[x-15], by: 7) ^ rotateRight(M[x-15], by: 18) ^ (M[x-15] >> 3)
                 let s0 = rotateRight(M[x-15], by: 7) ^ rotateRight(M[x-15], by: 18) ^ (M[x-15] >> 3)