mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
982 B
Swift
24 lines
982 B
Swift
4 years ago
|
import Curve25519Kit
|
||
5 years ago
|
|
||
|
public extension ECKeyPair {
|
||
|
|
||
4 years ago
|
@objc var hexEncodedPrivateKey: String {
|
||
5 years ago
|
return privateKey.map { String(format: "%02hhx", $0) }.joined()
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
@objc var hexEncodedPublicKey: String {
|
||
5 years ago
|
// Prefixing with "05" is necessary for what seems to be a sort of Signal public key versioning system
|
||
5 years ago
|
return "05" + publicKey.map { String(format: "%02hhx", $0) }.joined()
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
@objc static func isValidHexEncodedPublicKey(candidate: String) -> Bool {
|
||
5 years ago
|
// Check that it's a valid hexadecimal encoding
|
||
|
let allowedCharacters = CharacterSet(charactersIn: "0123456789ABCDEF")
|
||
|
guard candidate.uppercased().unicodeScalars.allSatisfy({ allowedCharacters.contains($0) }) else { return false }
|
||
|
// 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
|
||
|
}
|
||
|
}
|