Add serialization
This commit is contained in:
@@ -34,3 +34,10 @@ export async function pubkey_fromString(pubkey_str: string, usage: Usage): Promi
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function payload_fromString(text: string): Uint8Array {
|
||||
return new TextEncoder().encode(text)
|
||||
}
|
||||
export function payload_toString(payload: Uint8Array): string {
|
||||
return new TextDecoder().decode(payload)
|
||||
}
|
||||
|
||||
+10
-3
@@ -1,8 +1,8 @@
|
||||
import logger from 'log'
|
||||
import SecretBox from './secret-box'
|
||||
import {DHusage, ecdh} from './kdf'
|
||||
import {pubkey_fromString, pubkey_toString, Usage} from './misc'
|
||||
import * as consts from './const'
|
||||
import * as misc from './misc.ts'
|
||||
|
||||
const log = logger('crypto:private-box')
|
||||
|
||||
@@ -39,7 +39,7 @@ export default class PrivateBox {
|
||||
|
||||
public async toString(): Promise<string> {
|
||||
log.trace('toString')
|
||||
const pubkey = await pubkey_toString(this.pubkey)
|
||||
const pubkey = await PrivateBox.pubkey_toString(this.pubkey)
|
||||
const box = this.box.toString()
|
||||
return `${pubkey}.${box}`
|
||||
}
|
||||
@@ -49,11 +49,18 @@ export default class PrivateBox {
|
||||
const parts = data.split('.')
|
||||
if (parts.length < 2) return null
|
||||
|
||||
const pubkey = await pubkey_fromString(parts[0], Usage.ecdh)
|
||||
const pubkey = await PrivateBox.pubkey_fromString(parts[0])
|
||||
if (pubkey === null) return null
|
||||
const box = SecretBox.fromString(parts.slice(1).join('.'))
|
||||
if (box === null) return null
|
||||
|
||||
return new PrivateBox(pubkey, box)
|
||||
}
|
||||
|
||||
public static pubkey_toString(pubkey: CryptoKey): Promise<string> {
|
||||
return misc.pubkey_toString(pubkey)
|
||||
}
|
||||
public static pubkey_fromString(pubkey: string): Promise<CryptoKey | null> {
|
||||
return misc.pubkey_fromString(pubkey, misc.Usage.ecdh)
|
||||
}
|
||||
}
|
||||
|
||||
+10
-3
@@ -1,7 +1,7 @@
|
||||
import logger from 'log'
|
||||
import {DHusage, ecdh} from './kdf'
|
||||
import SecretWrap from './secret-wrap'
|
||||
import {pubkey_fromString, pubkey_toString, Usage} from './misc'
|
||||
import * as misc from './misc'
|
||||
import * as consts from './const'
|
||||
|
||||
const log = logger('crypto:private-wrap')
|
||||
@@ -32,7 +32,7 @@ export default class PrivateWrap {
|
||||
|
||||
public async toString(): Promise<string> {
|
||||
log.trace('toString')
|
||||
const pubkey = await pubkey_toString(this.pubkey)
|
||||
const pubkey = await PrivateWrap.pubkey_toString(this.pubkey)
|
||||
const box = this.box.toString()
|
||||
return `${pubkey}.${box}`
|
||||
}
|
||||
@@ -42,11 +42,18 @@ export default class PrivateWrap {
|
||||
const parts = data.split('.')
|
||||
if (parts.length < 2) return null
|
||||
|
||||
const pubkey = await pubkey_fromString(parts[0], Usage.ecdh)
|
||||
const pubkey = await PrivateWrap.pubkey_fromString(parts[0])
|
||||
if (pubkey === null) return null
|
||||
const box = SecretWrap.fromString(parts.slice(1).join('.'))
|
||||
if (box === null) return null
|
||||
|
||||
return new PrivateWrap(box, pubkey)
|
||||
}
|
||||
|
||||
public static pubkey_toString(pubkey: CryptoKey): Promise<string> {
|
||||
return misc.pubkey_toString(pubkey)
|
||||
}
|
||||
public static pubkey_fromString(pubkey: string): Promise<CryptoKey | null> {
|
||||
return misc.pubkey_fromString(pubkey, misc.Usage.ecdh)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import logger from 'log'
|
||||
const log = logger('crypto:signature')
|
||||
import * as consts from './const'
|
||||
import * as misc from './misc'
|
||||
import * as libmisc from 'misc'
|
||||
|
||||
export async function gen(extractable: boolean = true): Promise<CryptoKeyPair> {
|
||||
return crypto.subtle.generateKey(consts.SIGNATURE_KEY, extractable, ['sign', 'verify'])
|
||||
@@ -14,3 +16,17 @@ export async function verify(message: Uint8Array, pubkey: CryptoKey, signature:
|
||||
log.trace('verify')
|
||||
return crypto.subtle.verify(consts.SIGNATURE_ALGO, pubkey, signature, message)
|
||||
}
|
||||
|
||||
export function pubkey_toString(pubkey: CryptoKey): Promise<string> {
|
||||
return misc.pubkey_toString(pubkey)
|
||||
}
|
||||
export async function pubkey_fromString(pubkey: string): Promise<CryptoKey | null> {
|
||||
return misc.pubkey_fromString(pubkey, misc.Usage.sign)
|
||||
}
|
||||
|
||||
export function signature_toString(signature: Uint8Array): string {
|
||||
return libmisc.a2b64(signature)
|
||||
}
|
||||
export function signature_fromString(signature: string): Uint8Array | null {
|
||||
return libmisc.b642a(signature)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user