Bläddra i källkod

Use unmanaged memory buffer in SHA2

Marcin Krzyżanowski 8 år sedan
förälder
incheckning
1040ce02c5
1 ändrade filer med 7 tillägg och 2 borttagningar
  1. 7 2
      Sources/CryptoSwift/SHA2.swift

+ 7 - 2
Sources/CryptoSwift/SHA2.swift

@@ -211,8 +211,10 @@ public final class SHA2: DigestType {
     fileprivate func process32(block chunk: ArraySlice<UInt8>, currentHash hh: inout Array<UInt32>) {
         // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15, big-endian
         // Extend the sixteen 32-bit words into sixty-four 32-bit words:
-        var M = Array<UInt32>(repeating: 0, count: self.k.count)
-        for x in 0 ..< M.count {
+        let M = UnsafeMutablePointer<UInt32>.allocate(capacity: self.k.count)
+        M.initialize(to: 0, count: self.k.count)
+
+        for x in 0 ..< self.k.count {
             switch (x) {
             case 0 ... 15:
                 let start = chunk.startIndex.advanced(by: x * 4) // * MemoryLayout<UInt32>.size
@@ -262,6 +264,9 @@ public final class SHA2: DigestType {
         hh[5] = hh[5] &+ F
         hh[6] = hh[6] &+ G
         hh[7] = hh[7] &+ H
+
+        M.deinitialize(count: self.k.count)
+        M.deallocate(capacity: self.k.count)
     }
 }