restore cryptokey class with node ref

This commit is contained in:
Kyle Spearrin 2018-01-09 15:38:33 -05:00
parent bf35cf3588
commit 9a74f3e4ea
1 changed files with 6 additions and 7 deletions

View File

@ -1,3 +1,5 @@
import * as forge from 'node-forge';
import { EncryptionType } from '../../enums/encryptionType'; import { EncryptionType } from '../../enums/encryptionType';
import { SymmetricCryptoKeyBuffers } from './symmetricCryptoKeyBuffers'; import { SymmetricCryptoKeyBuffers } from './symmetricCryptoKeyBuffers';
@ -11,20 +13,17 @@ export class SymmetricCryptoKey {
macKey: string; macKey: string;
encType: EncryptionType; encType: EncryptionType;
keyBuf: SymmetricCryptoKeyBuffers; keyBuf: SymmetricCryptoKeyBuffers;
forge: any;
constructor(keyBytes: string, b64KeyBytes?: boolean, encType?: EncryptionType) { constructor(keyBytes: string, b64KeyBytes?: boolean, encType?: EncryptionType) {
this.forge = (window as any).forge;
if (b64KeyBytes) { if (b64KeyBytes) {
keyBytes = this.forge.util.decode64(keyBytes); keyBytes = forge.util.decode64(keyBytes);
} }
if (!keyBytes) { if (!keyBytes) {
throw new Error('Must provide keyBytes'); throw new Error('Must provide keyBytes');
} }
const buffer = this.forge.util.createBuffer(keyBytes); const buffer = (forge as any).util.createBuffer(keyBytes);
if (!buffer || buffer.length() === 0) { if (!buffer || buffer.length() === 0) {
throw new Error('Couldn\'t make buffer'); throw new Error('Couldn\'t make buffer');
} }
@ -42,7 +41,7 @@ export class SymmetricCryptoKey {
} }
this.key = keyBytes; this.key = keyBytes;
this.keyB64 = this.forge.util.encode64(keyBytes); this.keyB64 = forge.util.encode64(keyBytes);
this.encType = encType; this.encType = encType;
if (encType === EncryptionType.AesCbc256_B64 && bufferLength === 32) { if (encType === EncryptionType.AesCbc256_B64 && bufferLength === 32) {
@ -59,7 +58,7 @@ export class SymmetricCryptoKey {
} }
} }
getBuffers() { getBuffers(): SymmetricCryptoKeyBuffers {
if (this.keyBuf) { if (this.keyBuf) {
return this.keyBuf; return this.keyBuf;
} }