squash
fix android module name import fixes fix some imports use `@atproto/jwk` over local zod schema preview oauth client rename package alignment more alignment database implementation cleanup some types alignment switch implementation for es256 half of `verifyJwt` for android half of `verifyJwt` for iOS rm log `createJwt` android `createJwt` ios rm print cleanup native impl silence some warnings reorg, finish web impl follow the `CryptoImplementation` move some more things around normalize some names rename func add generate on android add android dep rm bool add alg better implementation ios jwk generation fix index.ts add `getRandomValues()` on native web `getRandomValues` silence ts error for missing crypto on web add `digest` for web add `digest` add module
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import CryptoKit
|
||||
import JOSESwift
|
||||
|
||||
class CryptoUtil {
|
||||
// The equivalent of crypto.subtle.digest() with JS on web
|
||||
public static func digest(data: Data) -> Data {
|
||||
let hash = SHA256.hash(data: data)
|
||||
return Data(hash)
|
||||
}
|
||||
|
||||
public static func getRandomValues(byteLength: Int) -> Data {
|
||||
let bytes = (0..<byteLength).map { _ in UInt8.random(in: UInt8.min...UInt8.max) }
|
||||
return Data(bytes)
|
||||
}
|
||||
|
||||
public static func generateKeyPair(kid: String?) throws -> (publicJWK: JWK, privateJWK: JWK)? {
|
||||
let keyIdString = kid ?? UUID().uuidString
|
||||
|
||||
let privateKey = P256.Signing.PrivateKey()
|
||||
let publicKey = privateKey.publicKey
|
||||
|
||||
let x = publicKey.x963Representation[1..<33].base64URLEncodedString()
|
||||
let y = publicKey.x963Representation[33...].base64URLEncodedString()
|
||||
let d = privateKey.rawRepresentation.base64URLEncodedString()
|
||||
|
||||
let publicJWK = JWK(kty: "EC", use: "sig", crv: "P-256", kid: keyIdString, x: x, y: y, alg: "ES256")
|
||||
let privateJWK = JWK(kty: "EC", use: "sig", crv: "P-256", kid: keyIdString, x: x, y: y, d: d, alg: "ES256")
|
||||
|
||||
return (publicJWK, privateJWK)
|
||||
}
|
||||
}
|
||||
|
||||
extension Data {
|
||||
func base64URLEncodedString() -> String {
|
||||
return self.base64EncodedString().replacingOccurrences(of: "+", with: "-").replacingOccurrences(of: "/", with: "_").replacingOccurrences(of: "=", with: "")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user