Fix/Update #1
| @ -1,53 +0,0 @@ | |||||||
| /** |  | ||||||
|  * Author: Hugo Pointcheval |  | ||||||
|  * Email: git@pcl.ovh |  | ||||||
|  * ----- |  | ||||||
|  * File: Cipher.swift |  | ||||||
|  * Created Date: 25/12/2021 18:31:28 |  | ||||||
|  * Last Modified: 25/12/2021 18:38:53 |  | ||||||
|  * ----- |  | ||||||
|  * Copyright (c) 2021 |  | ||||||
|  */ |  | ||||||
| 
 |  | ||||||
| import Foundation |  | ||||||
| import CryptoKit |  | ||||||
| 
 |  | ||||||
| class AESCipher { |  | ||||||
|     /// Encrypts plaintext with key using AES GCM |  | ||||||
|     @available(iOS 13.0, *) |  | ||||||
|     static func encrypt(plaintext: Data, key: Data) -> Data? { |  | ||||||
|         let symmetricKey = SymmetricKey.init(data: key) |  | ||||||
|         let encrypted = try? AES.GCM.seal(plaintext, using: symmetricKey) |  | ||||||
|         return encrypted?.combined |  | ||||||
|     } |  | ||||||
|      |  | ||||||
|     /// Decrypts ciphertext with key using AES GCM |  | ||||||
|     @available(iOS 13.0, *) |  | ||||||
|     static func decrypt(ciphertext: Data, key: Data) -> Data? { |  | ||||||
|         let symmetricKey = SymmetricKey.init(data: key) |  | ||||||
|         let sealedBox = try? AES.GCM.SealedBox(combined: ciphertext) |  | ||||||
|         if (sealedBox == nil) { return nil } |  | ||||||
|         let decryptedData = try? AES.GCM.open(sealedBox!, using: symmetricKey) |  | ||||||
|         return decryptedData |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| class CHACHACipher { |  | ||||||
|     /// Encrypts plaintext with key using CHACHAPOLY |  | ||||||
|     @available(iOS 13.0, *) |  | ||||||
|     static func encrypt(plaintext: Data, key: Data) -> Data? { |  | ||||||
|         let symmetricKey = SymmetricKey.init(data: key) |  | ||||||
|         let encrypted = try? ChaChaPoly.seal(plaintext, using: symmetricKey) |  | ||||||
|         return encrypted?.combined |  | ||||||
|     } |  | ||||||
|      |  | ||||||
|     /// Decrypts ciphertext with key using CHACHAPOLY |  | ||||||
|     @available(iOS 13.0, *) |  | ||||||
|     static func decrypt(ciphertext: Data, key: Data) -> Data? { |  | ||||||
|         let symmetricKey = SymmetricKey.init(data: key) |  | ||||||
|         let sealedBox = try? ChaChaPoly.SealedBox(combined: ciphertext) |  | ||||||
|         if (sealedBox == nil) { return nil } |  | ||||||
|         let decryptedData = try? ChaChaPoly.open(sealedBox!, using: symmetricKey) |  | ||||||
|         return decryptedData |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -1,43 +0,0 @@ | |||||||
| /** |  | ||||||
|  * Author: Hugo Pointcheval |  | ||||||
|  * Email: git@pcl.ovh |  | ||||||
|  * ----- |  | ||||||
|  * File: Hash.swift |  | ||||||
|  * Created Date: 25/12/2021 18:31:11 |  | ||||||
|  * Last Modified: 25/12/2021 18:38:20 |  | ||||||
|  * ----- |  | ||||||
|  * Copyright (c) 2021 |  | ||||||
|  */ |  | ||||||
| 
 |  | ||||||
| import Foundation |  | ||||||
| import CommonCrypto |  | ||||||
| import CryptoKit |  | ||||||
| 
 |  | ||||||
| enum HashAlgorithm: String { |  | ||||||
|     case HashSHA256 = "sha256" |  | ||||||
|     case HashSHA384 = "sha384" |  | ||||||
|     case HashSHA512 = "sha512" |  | ||||||
|      |  | ||||||
|     var commonCrypto: UInt32 { |  | ||||||
|         switch self { |  | ||||||
|         case .HashSHA256: return CCPBKDFAlgorithm(kCCPRFHmacAlgSHA256) |  | ||||||
|         case .HashSHA384: return CCPBKDFAlgorithm(kCCPRFHmacAlgSHA384) |  | ||||||
|         case .HashSHA512: return CCPBKDFAlgorithm(kCCPRFHmacAlgSHA512) |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| @available(iOS 13.0, *) |  | ||||||
| class Hash { |  | ||||||
|     /// Hash a message with a specified HashAlgorithm |  | ||||||
|     static func digest(data: Data, algorithm: HashAlgorithm) -> Data { |  | ||||||
|         switch algorithm { |  | ||||||
|             case .HashSHA256: |  | ||||||
|                 return Data(SHA256.hash(data: data)) |  | ||||||
|             case .HashSHA384: |  | ||||||
|                 return Data(SHA384.hash(data: data)) |  | ||||||
|             case .HashSHA512: |  | ||||||
|                 return Data(SHA512.hash(data: data)) |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -1,78 +0,0 @@ | |||||||
| /** |  | ||||||
|  * Author: Hugo Pointcheval |  | ||||||
|  * Email: git@pcl.ovh |  | ||||||
|  * ----- |  | ||||||
|  * File: KEM.swift |  | ||||||
|  * Created Date: 25/12/2021 18:31:48 |  | ||||||
|  * Last Modified: 25/12/2021 18:40:00 |  | ||||||
|  * ----- |  | ||||||
|  * Copyright (c) 2021 |  | ||||||
|  */ |  | ||||||
| 
 |  | ||||||
| import Foundation |  | ||||||
| import CryptoKit |  | ||||||
| 
 |  | ||||||
| class KeyPair { |  | ||||||
|     /// Generate a keypair. |  | ||||||
|     @available(iOS 13.0, *) |  | ||||||
|     static func fromCurve() -> Data { |  | ||||||
|         let sk = P256.KeyAgreement.PrivateKey() |  | ||||||
|         var kp = sk.rawRepresentation |  | ||||||
|         kp.append(contentsOf: sk.publicKey.rawRepresentation) |  | ||||||
|         return kp; |  | ||||||
|     } |  | ||||||
|      |  | ||||||
|     /// Import private key from Data |  | ||||||
|     @available(iOS 13.0, *) |  | ||||||
|     static func importPrivateKey(privateKey: Data) throws -> P256.KeyAgreement.PrivateKey { |  | ||||||
|         let sk = try P256.KeyAgreement.PrivateKey(rawRepresentation: privateKey) |  | ||||||
|          |  | ||||||
|         return sk; |  | ||||||
|     } |  | ||||||
|      |  | ||||||
|     /// Import public key from Data |  | ||||||
|     @available(iOS 13.0, *) |  | ||||||
|     static func importPublicKey(publicKey: Data) throws -> P256.KeyAgreement.PublicKey { |  | ||||||
|         let pk = try P256.KeyAgreement.PublicKey(rawRepresentation: publicKey) |  | ||||||
|          |  | ||||||
|         return pk; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| class ECDH { |  | ||||||
|     /// Generate a shared secret with your private key and other party public key. |  | ||||||
|     @available(iOS 13.0, *) |  | ||||||
|     static func generateSharedSecretKey(salt: Data, hash: HashAlgorithm, keyBytesCount: Int ,privateKey: Data, publicKey: Data) -> Data? { |  | ||||||
|         let sk = try? KeyPair.importPrivateKey(privateKey: privateKey) |  | ||||||
|         if (sk == nil) {return nil} |  | ||||||
|          |  | ||||||
|         let pk = try? KeyPair.importPublicKey(publicKey: publicKey) |  | ||||||
|         if (pk == nil) {return nil} |  | ||||||
| 
 |  | ||||||
|         let secret = try? sk!.sharedSecretFromKeyAgreement(with: pk!) |  | ||||||
|          |  | ||||||
|         switch hash { |  | ||||||
|         case .HashSHA256: |  | ||||||
|             let key = secret?.hkdfDerivedSymmetricKey(using: SHA256.self, salt: salt, sharedInfo: Data(), outputByteCount: keyBytesCount) |  | ||||||
|             if (key == nil) { |  | ||||||
|                 return nil |  | ||||||
|             } else { |  | ||||||
|                 return Key.toBytes(key: key!) |  | ||||||
|             } |  | ||||||
|         case .HashSHA384: |  | ||||||
|             let key = secret?.hkdfDerivedSymmetricKey(using: SHA384.self, salt: salt, sharedInfo: Data(), outputByteCount: keyBytesCount) |  | ||||||
|             if (key == nil) { |  | ||||||
|                 return nil |  | ||||||
|             } else { |  | ||||||
|                 return Key.toBytes(key: key!) |  | ||||||
|             } |  | ||||||
|         case .HashSHA512: |  | ||||||
|             let key = secret?.hkdfDerivedSymmetricKey(using: SHA512.self, salt: salt, sharedInfo: Data(), outputByteCount: keyBytesCount) |  | ||||||
|             if (key == nil) { |  | ||||||
|                 return nil |  | ||||||
|             } else { |  | ||||||
|                 return Key.toBytes(key: key!) |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -1,62 +0,0 @@ | |||||||
| /** |  | ||||||
|  * Author: Hugo Pointcheval |  | ||||||
|  * Email: git@pcl.ovh |  | ||||||
|  * ----- |  | ||||||
|  * File: KDF.swift |  | ||||||
|  * Created Date: 25/12/2021 17:45:28 |  | ||||||
|  * Last Modified: 25/12/2021 17:45:38 |  | ||||||
|  * ----- |  | ||||||
|  * Copyright (c) 2021 |  | ||||||
|  */ |  | ||||||
| 
 |  | ||||||
| import Foundation |  | ||||||
| import CryptoKit |  | ||||||
| import CommonCrypto |  | ||||||
| 
 |  | ||||||
| class Key { |  | ||||||
|     /// Generate secret key of a specified length |  | ||||||
|     @available(iOS 13.0, *) |  | ||||||
|     static func fromSecureRandom(bitsCount : Int) -> Data { |  | ||||||
|         let symmetricKey = SymmetricKey.init(size: SymmetricKeySize(bitCount: bitsCount)) |  | ||||||
|         return toBytes(key: symmetricKey) |  | ||||||
|     } |  | ||||||
|      |  | ||||||
|     /// Encode key as Data |  | ||||||
|     @available(iOS 13.0, *) |  | ||||||
|     static func toBytes(key: SymmetricKey) -> Data { |  | ||||||
|         let keyBytes = key.withUnsafeBytes |  | ||||||
|             { |  | ||||||
|                 return Data(Array($0)) |  | ||||||
|             } |  | ||||||
|         return keyBytes |  | ||||||
|     } |  | ||||||
|      |  | ||||||
|     /// Derive a new secret key with PBKDF2 algorithm |  | ||||||
|     static func fromPBKDF2(password: String, salt: String, keyBytesCount: Int, iterations: Int, algorithm: HashAlgorithm) -> Data? { |  | ||||||
|         let passwordData = password.data(using: .utf8)! |  | ||||||
|         let saltData = salt.data(using: .utf8)! |  | ||||||
|          |  | ||||||
|         var derivedKeyData = Data(repeating: 0, count: keyBytesCount) |  | ||||||
|         let localDerivedKeyData = derivedKeyData |  | ||||||
|          |  | ||||||
|         let status = derivedKeyData.withUnsafeMutableBytes { (derivedKeyBytes: UnsafeMutableRawBufferPointer) in |  | ||||||
|             saltData.withUnsafeBytes { (saltBytes: UnsafeRawBufferPointer) in |  | ||||||
|                 CCKeyDerivationPBKDF( |  | ||||||
|                     CCPBKDFAlgorithm(kCCPBKDF2), |  | ||||||
|                     password, |  | ||||||
|                     passwordData.count, |  | ||||||
|                     saltBytes.bindMemory(to: UInt8.self).baseAddress, |  | ||||||
|                     saltData.count, |  | ||||||
|                     algorithm.commonCrypto, |  | ||||||
|                     UInt32(iterations), |  | ||||||
|                     derivedKeyBytes.bindMemory(to: UInt8.self).baseAddress, |  | ||||||
|                     localDerivedKeyData.count) |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|         if (status != kCCSuccess) { |  | ||||||
|             return nil; |  | ||||||
|         } |  | ||||||
|          |  | ||||||
|         return derivedKeyData |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -0,0 +1,28 @@ | |||||||
|  | // | ||||||
|  | //  AES.swift | ||||||
|  | //  native_crypto_ios | ||||||
|  | // | ||||||
|  | //  Created by Hugo Pointcheval on 25/05/2022. | ||||||
|  | // | ||||||
|  | 
 | ||||||
|  | import Foundation | ||||||
|  | 
 | ||||||
|  | class AES : Cipher { | ||||||
|  |     /// Encrypts plaintext with key using AES GCM | ||||||
|  |     @available(iOS 13.0, *) | ||||||
|  |     static func encrypt(plaintext: Data, key: Data) -> Data? { | ||||||
|  |         let symmetricKey = SymmetricKey.init(data: key) | ||||||
|  |         let encrypted = try? AES.GCM.seal(plaintext, using: symmetricKey) | ||||||
|  |         return encrypted?.combined | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     /// Decrypts ciphertext with key using AES GCM | ||||||
|  |     @available(iOS 13.0, *) | ||||||
|  |     static func decrypt(ciphertext: Data, key: Data) -> Data? { | ||||||
|  |         let symmetricKey = SymmetricKey.init(data: key) | ||||||
|  |         let sealedBox = try? AES.GCM.SealedBox(combined: ciphertext) | ||||||
|  |         if (sealedBox == nil) { return nil } | ||||||
|  |         let decryptedData = try? AES.GCM.open(sealedBox!, using: symmetricKey) | ||||||
|  |         return decryptedData | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										8
									
								
								packages/native_crypto_ios/ios/Classes/kdf/Pbkdf2.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								packages/native_crypto_ios/ios/Classes/kdf/Pbkdf2.swift
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,8 @@ | |||||||
|  | // | ||||||
|  | //  Pbkdf2.swift | ||||||
|  | //  native_crypto_ios | ||||||
|  | // | ||||||
|  | //  Created by Hugo Pointcheval on 25/05/2022. | ||||||
|  | // | ||||||
|  | 
 | ||||||
|  | import Foundation | ||||||
| @ -0,0 +1,8 @@ | |||||||
|  | // | ||||||
|  | //  SecretKey.swift | ||||||
|  | //  native_crypto_ios | ||||||
|  | // | ||||||
|  | //  Created by Hugo Pointcheval on 25/05/2022. | ||||||
|  | // | ||||||
|  | 
 | ||||||
|  | import Foundation | ||||||
| @ -0,0 +1,8 @@ | |||||||
|  | // | ||||||
|  | //  Cipher.swift | ||||||
|  | //  native_crypto_ios | ||||||
|  | // | ||||||
|  | //  Created by Hugo Pointcheval on 25/05/2022. | ||||||
|  | // | ||||||
|  | 
 | ||||||
|  | import Foundation | ||||||
| @ -0,0 +1,8 @@ | |||||||
|  | // | ||||||
|  | //  Key.swift | ||||||
|  | //  native_crypto_ios | ||||||
|  | // | ||||||
|  | //  Created by Hugo Pointcheval on 25/05/2022. | ||||||
|  | // | ||||||
|  | 
 | ||||||
|  | import Foundation | ||||||
| @ -0,0 +1,8 @@ | |||||||
|  | // | ||||||
|  | //  KeyDerivation.swift | ||||||
|  | //  native_crypto_ios | ||||||
|  | // | ||||||
|  | //  Created by Hugo Pointcheval on 25/05/2022. | ||||||
|  | // | ||||||
|  | 
 | ||||||
|  | import Foundation | ||||||
| @ -0,0 +1,8 @@ | |||||||
|  | // | ||||||
|  | //  CipherAlgorithm.swift | ||||||
|  | //  native_crypto_ios | ||||||
|  | // | ||||||
|  | //  Created by Hugo Pointcheval on 25/05/2022. | ||||||
|  | // | ||||||
|  | 
 | ||||||
|  | import Foundation | ||||||
| @ -0,0 +1,8 @@ | |||||||
|  | // | ||||||
|  | //  HashAlgorithm.swift | ||||||
|  | //  native_crypto_ios | ||||||
|  | // | ||||||
|  | //  Created by Hugo Pointcheval on 25/05/2022. | ||||||
|  | // | ||||||
|  | 
 | ||||||
|  | import Foundation | ||||||
| @ -0,0 +1,8 @@ | |||||||
|  | // | ||||||
|  | //  KdfAlgorithm.swift | ||||||
|  | //  native_crypto_ios | ||||||
|  | // | ||||||
|  | //  Created by Hugo Pointcheval on 25/05/2022. | ||||||
|  | // | ||||||
|  | 
 | ||||||
|  | import Foundation | ||||||
| @ -0,0 +1,8 @@ | |||||||
|  | // | ||||||
|  | //  NativeCryptoError.swift | ||||||
|  | //  native_crypto_ios | ||||||
|  | // | ||||||
|  | //  Created by Hugo Pointcheval on 25/05/2022. | ||||||
|  | // | ||||||
|  | 
 | ||||||
|  | import Foundation | ||||||
							
								
								
									
										8
									
								
								packages/native_crypto_ios/ios/Classes/utils/Task.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								packages/native_crypto_ios/ios/Classes/utils/Task.swift
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,8 @@ | |||||||
|  | // | ||||||
|  | //  Task.swift | ||||||
|  | //  native_crypto_ios | ||||||
|  | // | ||||||
|  | //  Created by Hugo Pointcheval on 25/05/2022. | ||||||
|  | // | ||||||
|  | 
 | ||||||
|  | import Foundation | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user