Skip to content

Instantly share code, notes, and snippets.

@cardosoedgar
Created June 29, 2020 01:13
Show Gist options
  • Save cardosoedgar/665c07da004734153093b4e5dafe6ab4 to your computer and use it in GitHub Desktop.
Save cardosoedgar/665c07da004734153093b4e5dafe6ab4 to your computer and use it in GitHub Desktop.
let ellipticCurveHeader = [UInt8]([0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48,
0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03,
0x01, 0x07, 0x03, 0x42, 0x00])
let begin = "-----BEGIN PUBLIC KEY-----\n"
let end = "\n-----END PUBLIC KEY-----"
func retrivePublicKey() -> String? {
var error: Unmanaged<CFError>?
if let publicKey = getPublicKey(),
let keyRef = SecKeyCopyExternalRepresentation(publicKey, &error) {
var keyData = Data()
keyData.append(Data(ellipticCurveHeader))
keyData.append(keyRef as Data)
let b64Key = keyData.base64EncodedString(options: [.lineLength64Characters,
.endLineWithCarriageReturn])
return "\(begin)\(b64Key)\(end)"
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment