Sfoglia il codice sorgente

Reserve right amount of memory for result array.

Marcin Krzyżanowski 8 anni fa
parent
commit
cc6eb38341
2 ha cambiato i file con 2 aggiunte e 3 eliminazioni
  1. 1 1
      Sources/CryptoSwift/MD5.swift
  2. 1 2
      Sources/CryptoSwift/SHA2.swift

+ 1 - 1
Sources/CryptoSwift/MD5.swift

@@ -130,7 +130,7 @@ extension MD5: Updatable {
 
         // output current hash
         var result = Array<UInt8>()
-        result.reserveCapacity(self.accumulatedHash.count / 4)
+        result.reserveCapacity(MD5.digestLength)
 
         for hElement in self.accumulatedHash {
             let hLE = hElement.littleEndian

+ 1 - 2
Sources/CryptoSwift/SHA2.swift

@@ -286,16 +286,15 @@ extension SHA2: Updatable {
 
         // output current hash
         var result = Array<UInt8>()
+        result.reserveCapacity(self.variant.digestLength)
 
         switch self.variant {
             case .sha224, .sha256:
-                result.reserveCapacity(self.accumulatedHash32.count / 4)
                 self.variant.resultingArray(self.accumulatedHash32).forEach { //TODO: rename resultingArray -> resultSlice
                     let item = $0.bigEndian
                     result += [UInt8(item & 0xff), UInt8((item >> 8) & 0xff), UInt8((item >> 16) & 0xff), UInt8((item >> 24) & 0xff)]
             }
             case .sha384, .sha512:
-                result.reserveCapacity(self.accumulatedHash64.count / 4)
                 self.variant.resultingArray(self.accumulatedHash64).forEach {
                     let item = $0.bigEndian
                     var partialResult = Array<UInt8>()