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.
session-ios/SessionUtilitiesKit/Crypto/ECKeyPair+Hexadecimal.swift

23 lines
928 B
Swift

4 years ago
import Curve25519Kit
public extension ECKeyPair {
5 years ago
@objc var hexEncodedPrivateKey: String {
return privateKey.map { String(format: "%02hhx", $0) }.joined()
}
5 years ago
@objc var hexEncodedPublicKey: String {
// Prefixing with 'IdPrefix.standard' is necessary for what seems to be a sort of Signal public key versioning system
return IdPrefix.standard.rawValue + publicKey.map { String(format: "%02hhx", $0) }.joined()
}
@objc static func isValidHexEncodedPublicKey(candidate: String) -> Bool {
// Check that it's a valid hexadecimal encoding
guard Hex.isValid(candidate) else { return false }
// Check that it has length 66 and a valid prefix
guard candidate.count == 66 && IdPrefix.allCases.first(where: { candidate.hasPrefix($0.rawValue) }) != nil else { return false }
// It appears to be a valid public key
return true
}
}