private-wrap : Export pubKey serialization
This commit is contained in:
+17
-6
@@ -34,8 +34,7 @@ export class PrivateWrap {
|
||||
|
||||
public async toString(): Promise<string> {
|
||||
log.trace('toString')
|
||||
const pubkey_spki = await crypto.subtle.exportKey('spki', this.pubkey)
|
||||
const pubkey = a2b64(new Uint8Array(pubkey_spki))
|
||||
const pubkey = await PrivateWrap.publicKey_toString(this.pubkey)
|
||||
const box = this.box.toString()
|
||||
return `${pubkey}.${box}`
|
||||
}
|
||||
@@ -45,13 +44,25 @@ export class PrivateWrap {
|
||||
const parts = data.split('.', 2)
|
||||
if (parts.length !== 2) return null
|
||||
|
||||
const pubkey_str = b642a(parts[0])
|
||||
if (pubkey_str.is_err()) return null
|
||||
const pubkey = await crypto.subtle.importKey('spki', pubkey_str.unwrap(), algorithm, true, [])
|
||||
|
||||
const pubkey = await this.publicKey_fromString(parts[0])
|
||||
if (pubkey === null) return null
|
||||
const box = SecretWrap.fromString(parts[1])
|
||||
if (box === null) return null
|
||||
|
||||
return new PrivateWrap(box, pubkey)
|
||||
}
|
||||
|
||||
public static async publicKey_toString(publicKey: CryptoKey): Promise<string> {
|
||||
const pubkey_spki = await crypto.subtle.exportKey('spki', publicKey)
|
||||
return a2b64(new Uint8Array(pubkey_spki))
|
||||
}
|
||||
public static async publicKey_fromString(data: string): Promise<CryptoKey | null> {
|
||||
const pubkey_str = b642a(data)
|
||||
if (pubkey_str.is_err()) return null
|
||||
try {
|
||||
return crypto.subtle.importKey('spki', pubkey_str.unwrap(), algorithm, true, [])
|
||||
} catch(e) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user