1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-25 16:59:17 +01:00

crypto key

This commit is contained in:
Kyle Spearrin 2017-11-01 23:35:58 -04:00
parent c073d74e04
commit 53824a02a2

View File

@ -0,0 +1,88 @@
import { EncryptionType } from '../../enums/encryptionType.enum';
import UtilsService from '../../services/utils.service';
export default class SymmetricCryptoKey {
key: string;
keyB64: string;
encKey: string;
macKey: string;
encType: EncryptionType;
keyBuf: SymmetricCryptoKeyBuffers;
constructor(keyBytes: string, b64KeyBytes?: boolean, encType?: EncryptionType) {
let win = (window as any);
if (b64KeyBytes) {
keyBytes = win.forge.util.decode64(keyBytes);
}
if (!keyBytes) {
throw new Error('Must provide keyBytes');
}
let buffer = win.forge.util.createBuffer(keyBytes);
if (!buffer || buffer.length() === 0) {
throw new Error('Couldn\'t make buffer');
}
let bufferLength: number = buffer.length();
if (encType == null) {
if (bufferLength == 32) {
encType = EncryptionType.AesCbc256_B64;
} else if (bufferLength == 64) {
encType = EncryptionType.AesCbc256_HmacSha256_B64;
} else {
throw new Error('Unable to determine encType.');
}
}
this.key = keyBytes;
this.keyB64 = win.forge.util.encode64(keyBytes);
this.encType = encType;
if (encType === EncryptionType.AesCbc256_B64 && bufferLength === 32) {
this.encKey = keyBytes;
this.macKey = null;
} else if (encType === EncryptionType.AesCbc128_HmacSha256_B64 && bufferLength === 32) {
this.encKey = buffer.getBytes(16); // first half
this.macKey = buffer.getBytes(16); // second half
} else if (encType === EncryptionType.AesCbc256_HmacSha256_B64 && bufferLength === 64) {
this.encKey = buffer.getBytes(32); // first half
this.macKey = buffer.getBytes(32); // second half
} else {
throw new Error('Unsupported encType/key length.');
}
}
getBuffers() {
if (this.keyBuf) {
return this.keyBuf;
}
const key = UtilsService.fromB64ToArray(this.keyB64);
let keys = new SymmetricCryptoKeyBuffers(key.buffer);
if (this.macKey) {
keys.encKey = key.slice(0, key.length / 2).buffer;
keys.macKey = key.slice(key.length / 2).buffer;
} else {
keys.encKey = key.buffer;
keys.macKey = null;
}
this.keyBuf = keys;
return this.keyBuf;
}
}
class SymmetricCryptoKeyBuffers {
key: ArrayBuffer;
encKey?: ArrayBuffer;
macKey?: ArrayBuffer;
constructor(key: ArrayBuffer) {
this.key = key;
}
}