This commit is contained in:
+6
-6
@@ -17,17 +17,17 @@ export default class PrivateWrap {
|
||||
return crypto.subtle.generateKey(consts.ECDH, extractable, ['deriveKey']) as Promise<CryptoKeyPair>
|
||||
}
|
||||
|
||||
public static async encrypt(data: CryptoKey, pubkey: CryptoKey): Promise<PrivateWrap> {
|
||||
log.trace('encrypt')
|
||||
public static async wrap(data: CryptoKey, pubkey: CryptoKey): Promise<PrivateWrap> {
|
||||
log.trace('wrap')
|
||||
const tmp_keypair = await PrivateWrap.gen()
|
||||
const kd = await ecdh(tmp_keypair.privateKey, pubkey, DHusage.wrap)
|
||||
const box = await SecretWrap.encrypt(data, kd)
|
||||
const box = await SecretWrap.wrap(data, kd)
|
||||
return new this(box, tmp_keypair.publicKey)
|
||||
}
|
||||
public async decrypt(privkey: CryptoKey): Promise<CryptoKey | null> {
|
||||
log.trace('decrypt')
|
||||
public async unwrap(privkey: CryptoKey): Promise<CryptoKey | null> {
|
||||
log.trace('unwrap')
|
||||
const kd = await ecdh(privkey, this.pubkey, DHusage.wrap)
|
||||
return this.box.decrypt(kd)
|
||||
return this.box.unwrap(kd)
|
||||
}
|
||||
|
||||
public async toString(): Promise<string> {
|
||||
|
||||
+6
-6
@@ -16,17 +16,17 @@ export default class PwdWrap {
|
||||
return (await hkdf(k, Usage.wrap)) as CryptoKey
|
||||
}
|
||||
|
||||
public static async encrypt(data: CryptoKey, pwd: string, salt?: Uint8Array): Promise<PwdWrap> {
|
||||
log.trace('encrypt')
|
||||
public static async wrap(data: CryptoKey, pwd: string, salt?: Uint8Array): Promise<PwdWrap> {
|
||||
log.trace('wrap')
|
||||
salt = salt ?? crypto.getRandomValues(new Uint8Array(16))
|
||||
const k = await PwdWrap.derive(pwd, salt)
|
||||
const box = await SecretWrap.encrypt(data, k)
|
||||
const box = await SecretWrap.wrap(data, k)
|
||||
return new PwdWrap(box, salt)
|
||||
}
|
||||
public async decrypt(pwd: string): Promise<CryptoKey | null> {
|
||||
log.trace('decrypt')
|
||||
public async unwrap(pwd: string): Promise<CryptoKey | null> {
|
||||
log.trace('unwrap')
|
||||
const k = await PwdWrap.derive(pwd, this.salt)
|
||||
return this.box.decrypt(k)
|
||||
return this.box.unwrap(k)
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
|
||||
+4
-4
@@ -36,15 +36,15 @@ export default class SecretWrap {
|
||||
}
|
||||
}
|
||||
|
||||
public static async encrypt(data: CryptoKey, key: CryptoKey): Promise<SecretWrap> {
|
||||
log.trace('encrypt')
|
||||
public static async wrap(data: CryptoKey, key: CryptoKey): Promise<SecretWrap> {
|
||||
log.trace('wrap')
|
||||
const format = SecretWrap.format(data.type)
|
||||
const iv = crypto.getRandomValues(new Uint8Array(12))
|
||||
const box = await crypto.subtle.wrapKey(format, data, key, {name: consts.ENCRYPTION, iv})
|
||||
return new SecretWrap(new Uint8Array(box), data.algorithm, data.usages, format, iv)
|
||||
}
|
||||
public async decrypt(key: CryptoKey): Promise<CryptoKey | null> {
|
||||
log.trace('decrypt')
|
||||
public async unwrap(key: CryptoKey): Promise<CryptoKey | null> {
|
||||
log.trace('unwrap')
|
||||
try {
|
||||
return await crypto.subtle.unwrapKey(
|
||||
this.type,
|
||||
|
||||
Reference in New Issue
Block a user