Эх сурвалжийг харах

Add convenience callAsFunction API

Marcin Krzyzanowski 2 жил өмнө
parent
commit
d636cbf1c3

+ 4 - 0
Sources/CryptoSwift/HKDF.swift

@@ -85,4 +85,8 @@ public struct HKDF {
     }
     return Array(ret.prefix(self.dkLen))
   }
+
+  public func callAsFunction() throws -> Array<UInt8> {
+    try calculate()
+  }
 }

+ 5 - 0
Sources/CryptoSwift/MD5.swift

@@ -61,6 +61,11 @@ public final class MD5: DigestType {
     }
   }
 
+  public func callAsFunction(_ bytes: Array<UInt8>) -> Array<UInt8> {
+    calculate(for: bytes)
+  }
+
+
   // mutating currentHash in place is way faster than returning new result
   fileprivate func process(block chunk: ArraySlice<UInt8>, currentHash: inout Array<UInt32>) {
     assert(chunk.count == 16 * 4)

+ 4 - 0
Sources/CryptoSwift/PKCS/PBKDF1.swift

@@ -93,5 +93,9 @@ public extension PKCS5 {
       }
       return Array(t[0..<self.keyLength])
     }
+
+    public func callAsFunction() -> Array<UInt8> {
+      calculate()
+    }
   }
 }

+ 4 - 0
Sources/CryptoSwift/PKCS/PBKDF2.swift

@@ -83,6 +83,10 @@ public extension PKCS5 {
       }
       return Array(ret.prefix(self.dkLen))
     }
+
+    public func callAsFunction() throws -> Array<UInt8> {
+      try calculate()
+    }
   }
 }
 

+ 4 - 0
Sources/CryptoSwift/SHA1.swift

@@ -45,6 +45,10 @@ public final class SHA1: DigestType {
     }
   }
 
+  public func callAsFunction(_ bytes: Array<UInt8>) -> Array<UInt8> {
+    calculate(for: bytes)
+  }
+
   @usableFromInline
   func process(block chunk: ArraySlice<UInt8>, currentHash hh: inout ContiguousArray<UInt32>) {
     // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15, big-endian

+ 4 - 0
Sources/CryptoSwift/SHA2.swift

@@ -169,6 +169,10 @@ public final class SHA2: DigestType {
     }
   }
 
+  public func callAsFunction(_ bytes: Array<UInt8>) -> Array<UInt8> {
+    calculate(for: bytes)
+  }
+
   @usableFromInline
   func process64(block chunk: ArraySlice<UInt8>, currentHash hh: inout Array<UInt64>) {
     // break chunk into sixteen 64-bit words M[j], 0 ≤ j ≤ 15, big-endian

+ 4 - 0
Sources/CryptoSwift/SHA3.swift

@@ -96,6 +96,10 @@ public final class SHA3: DigestType {
     }
   }
 
+  public func callAsFunction(_ bytes: Array<UInt8>) -> Array<UInt8> {
+    calculate(for: bytes)
+  }
+
   ///  1. For all pairs (x,z) such that 0≤x<5 and 0≤z<w, let
   ///     C[x,z]=A[x, 0,z] ⊕ A[x, 1,z] ⊕ A[x, 2,z] ⊕ A[x, 3,z] ⊕ A[x, 4,z].
   ///  2. For all pairs (x, z) such that 0≤x<5 and 0≤z<w let

+ 5 - 0
Sources/CryptoSwift/Scrypt.swift

@@ -101,6 +101,11 @@ public final class Scrypt {
     let block = [UInt8](bufferPointer)
     return try PKCS5.PBKDF2(password: Array(self.password), salt: block, iterations: 1, keyLength: self.dkLen, variant: .sha2(.sha256)).calculate()
   }
+
+  public func callAsFunction() throws -> Array<UInt8> {
+    try calculate()
+  }
+
 }
 
 private extension Scrypt {