-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from internxt/add-hashing-native-methods
[_]: Add sha256 and sha512 native hash methods for iOS
- Loading branch information
Showing
13 changed files
with
276 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// Extensions.swift | ||
// RnCrypto | ||
// | ||
// Created by Robert on 1/7/22. | ||
// Copyright © 2022 Facebook. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
// Data extensions | ||
extension Data { | ||
struct HexEncodingOptions: OptionSet { | ||
let rawValue: Int | ||
static let upperCase = HexEncodingOptions(rawValue: 1 << 0) | ||
} | ||
|
||
func hexEncodedString(options: HexEncodingOptions = []) -> String { | ||
let format = options.contains(.upperCase) ? "%02hhX" : "%02hhx" | ||
return self.map { String(format: format, $0) }.joined() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// Hashes.swift | ||
// RnCrypto | ||
// | ||
// Created by Robert on 28/6/22. | ||
// Copyright © 2022 Facebook. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import IDZSwiftCommonCrypto | ||
|
||
|
||
enum HashInput { | ||
case message ([UInt8]) | ||
case messages (Array<[UInt8]>) | ||
} | ||
class RnCryptoHMAC { | ||
func sha256(inputs: Array<[UInt8]> ) -> [UInt8] { | ||
return toAlgorithm(inputs: inputs, algorithm: HMAC.Algorithm.sha256) | ||
} | ||
|
||
func sha512(inputs: Array<[UInt8]>) -> [UInt8] { | ||
return toAlgorithm(inputs: inputs, algorithm: HMAC.Algorithm.sha512) | ||
} | ||
|
||
private func toAlgorithm(inputs: Array<[UInt8]>, algorithm: HMAC.Algorithm) -> [UInt8] { | ||
let hash = HMAC(algorithm: algorithm, key: inputs[0]) | ||
for (index, message) in inputs.enumerated() { | ||
if index > 0 { | ||
_ = hash.update(message) | ||
} | ||
} | ||
return hash.final() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// KeyDerivation.swift | ||
// RnCrypto | ||
// | ||
// Created by Robert on 1/7/22. | ||
// Copyright © 2022 Facebook. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import IDZSwiftCommonCrypto | ||
class RnCryptoKeyDerivation { | ||
func pbkdf2(password: String, salt: String, rounds: Int, derivedKeyLength: Int) -> [UInt8] { | ||
return PBKDF.deriveKey(password: password, salt: salt, prf: PBKDF.PseudoRandomAlgorithm.sha512, rounds: uint(rounds), derivedKeyLength: UInt(derivedKeyLength)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.