Skip to content
Source
Sign inSign up

T3DpopKeys.swift

20 lines804 Bytes

1import CryptoKit
2import Foundation
3​
4/// P-256 keys for DPoP proofs, as 32-byte private scalars and 65-byte uncompressed public points.
5enum T3DpopKeys {
6 /// The private scalar followed by the public point.
7 static func generate() -> Data {
8 let key = P256.Signing.PrivateKey()
9 return key.rawRepresentation + key.publicKey.x963Representation
10 }
11​
12 static func matches(privateKey: Data, publicKey: Data) throws -> Bool {
13 try P256.Signing.PrivateKey(rawRepresentation: privateKey).publicKey.x963Representation == publicKey
14 }
15​
16 /// ES256 over the JWS signing input, as the raw `r‖s` pair JWS uses.
17 static func sign(privateKey: Data, signingInput: Data) throws -> Data {
18 try P256.Signing.PrivateKey(rawRepresentation: privateKey).signature(for: signingInput).rawRepresentation
19 }