session-ios/SessionUtilitiesKit/Crypto/ECKeyPair+Hexadecimal.swift

23 lines
841 B
Swift
Raw Permalink Normal View History

2020-11-25 06:15:16 +01:00
import Curve25519Kit
2020-11-11 00:58:56 +01:00
public extension ECKeyPair {
2020-11-19 05:24:09 +01:00
@objc var hexEncodedPrivateKey: String {
2020-11-12 00:41:45 +01:00
return privateKey.map { String(format: "%02hhx", $0) }.joined()
2020-11-11 00:58:56 +01:00
}
2020-11-19 05:24:09 +01:00
@objc var hexEncodedPublicKey: String {
2020-11-11 00:58:56 +01:00
// Prefixing with "05" is necessary for what seems to be a sort of Signal public key versioning system
2020-11-12 00:41:45 +01:00
return "05" + publicKey.map { String(format: "%02hhx", $0) }.joined()
2020-11-11 00:58:56 +01:00
}
2020-11-17 06:23:13 +01:00
@objc static func isValidHexEncodedPublicKey(candidate: String) -> Bool {
2020-11-11 00:58:56 +01:00
// Check that it's a valid hexadecimal encoding
2021-03-29 02:33:52 +02:00
guard Hex.isValid(candidate) else { return false }
2020-11-11 00:58:56 +01:00
// Check that it has length 66 and a leading "05"
guard candidate.count == 66 && candidate.hasPrefix("05") else { return false }
// It appears to be a valid public key
return true
}
}