mirror of
https://github.com/bitwarden/jslib
synced 2026-01-08 03:23:15 +00:00
Add encrypt and decrypt example of EncObject
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { EncObject } from "jslib-common/models/domain/encObject";
|
||||
|
||||
import { HashPurpose } from "../enums/hashPurpose";
|
||||
import { KdfType } from "../enums/kdfType";
|
||||
import { KeySuffixOptions } from "../enums/keySuffixOptions";
|
||||
@@ -75,12 +77,14 @@ export abstract class CryptoService {
|
||||
encKey?: SymmetricCryptoKey
|
||||
) => Promise<[SymmetricCryptoKey, EncString]>;
|
||||
encrypt: (plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey) => Promise<EncString>;
|
||||
encryptObject: <T>(plainValue: T, key?: SymmetricCryptoKey) => Promise<EncObject<T>>;
|
||||
encryptToBytes: (plainValue: ArrayBuffer, key?: SymmetricCryptoKey) => Promise<EncArrayBuffer>;
|
||||
rsaEncrypt: (data: ArrayBuffer, publicKey?: ArrayBuffer) => Promise<EncString>;
|
||||
rsaDecrypt: (encValue: string, privateKeyValue?: ArrayBuffer) => Promise<ArrayBuffer>;
|
||||
decryptToBytes: (encString: EncString, key?: SymmetricCryptoKey) => Promise<ArrayBuffer>;
|
||||
decryptToUtf8: (encString: EncString, key?: SymmetricCryptoKey) => Promise<string>;
|
||||
decryptFromBytes: (encBuf: ArrayBuffer, key: SymmetricCryptoKey) => Promise<ArrayBuffer>;
|
||||
decryptObject: <T>(encrypted: EncObject<T>, key?: SymmetricCryptoKey) => Promise<T>;
|
||||
randomNumber: (min: number, max: number) => Promise<number>;
|
||||
validateKey: (key: SymmetricCryptoKey) => Promise<boolean>;
|
||||
}
|
||||
|
||||
@@ -12,9 +12,9 @@ export class EncObject<T> {
|
||||
* @param mac
|
||||
*/
|
||||
constructor(
|
||||
private encryptionType: EncryptionType,
|
||||
private data: string,
|
||||
private iv: string,
|
||||
private mac: string
|
||||
public encryptionType: EncryptionType,
|
||||
public data: string,
|
||||
public iv: string,
|
||||
public mac: string
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import * as bigInt from "big-integer";
|
||||
|
||||
import { EncObject } from "jslib-common/models/domain/encObject";
|
||||
|
||||
import { CryptoService as CryptoServiceAbstraction } from "../abstractions/crypto.service";
|
||||
import { CryptoFunctionService } from "../abstractions/cryptoFunction.service";
|
||||
import { LogService } from "../abstractions/log.service";
|
||||
@@ -522,6 +524,39 @@ export class CryptoService implements CryptoServiceAbstraction {
|
||||
return new EncString(encObj.key.encType, data, iv, mac);
|
||||
}
|
||||
|
||||
async encryptObject<T>(obj: T, key?: SymmetricCryptoKey): Promise<EncObject<T>> {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const json = JSON.stringify(obj);
|
||||
const plainBuf = Utils.fromUtf8ToArray(json).buffer;
|
||||
|
||||
const encObj = await this.aesEncrypt(plainBuf, key);
|
||||
const iv = Utils.fromBufferToB64(encObj.iv);
|
||||
const data = Utils.fromBufferToB64(encObj.data);
|
||||
const mac = encObj.mac != null ? Utils.fromBufferToB64(encObj.mac) : null;
|
||||
return new EncObject(encObj.key.encType, data, iv, mac);
|
||||
}
|
||||
|
||||
async decryptObject<T>(obj: EncObject<T>, key?: SymmetricCryptoKey): Promise<T> {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const decryptedJson = await this.aesDecryptToUtf8(
|
||||
obj.encryptionType,
|
||||
obj.data,
|
||||
obj.iv,
|
||||
obj.mac,
|
||||
key
|
||||
);
|
||||
|
||||
const decrypted = JSON.parse(decryptedJson);
|
||||
|
||||
return decrypted;
|
||||
}
|
||||
|
||||
async encryptToBytes(plainValue: ArrayBuffer, key?: SymmetricCryptoKey): Promise<EncArrayBuffer> {
|
||||
const encValue = await this.aesEncrypt(plainValue, key);
|
||||
let macLen = 0;
|
||||
|
||||
Reference in New Issue
Block a user