1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-27 17:18:04 +01:00

byteString conversion without b64

This commit is contained in:
Kyle Spearrin 2018-04-17 22:35:53 -04:00
parent 3ca8716fc6
commit 2e58314759

View File

@ -20,10 +20,10 @@ export class WebCryptoFunctionService implements CryptoFunctionService {
iterations: number): Promise<ArrayBuffer> { iterations: number): Promise<ArrayBuffer> {
if (this.isEdge) { if (this.isEdge) {
const len = algorithm === 'sha256' ? 32 : 64; const len = algorithm === 'sha256' ? 32 : 64;
const passwordBytes = this.toForgeBytes(password); const passwordBytes = this.toByteString(password);
const saltBytes = this.toForgeBytes(salt); const saltBytes = this.toByteString(salt);
const derivedKeyBytes = (forge as any).pbkdf2(passwordBytes, saltBytes, iterations, len, algorithm); const derivedKeyBytes = (forge as any).pbkdf2(passwordBytes, saltBytes, iterations, len, algorithm);
return this.fromForgeBytesToBuf(derivedKeyBytes); return this.fromByteStringToBuf(derivedKeyBytes);
} }
const len = algorithm === 'sha256' ? 256 : 512; const len = algorithm === 'sha256' ? 256 : 512;
@ -52,9 +52,9 @@ export class WebCryptoFunctionService implements CryptoFunctionService {
md = (forge as any).md.sha512.create(); md = (forge as any).md.sha512.create();
} }
const valueBytes = this.toForgeBytes(value); const valueBytes = this.toByteString(value);
md.update(valueBytes, 'raw'); md.update(valueBytes, 'raw');
return this.fromForgeBytesToBuf(md.digest().data); return this.fromByteStringToBuf(md.digest().data);
} }
const valueBuf = this.toBuf(value); const valueBuf = this.toBuf(value);
@ -63,12 +63,12 @@ export class WebCryptoFunctionService implements CryptoFunctionService {
async hmac(value: ArrayBuffer, key: ArrayBuffer, algorithm: 'sha1' | 'sha256' | 'sha512'): Promise<ArrayBuffer> { async hmac(value: ArrayBuffer, key: ArrayBuffer, algorithm: 'sha1' | 'sha256' | 'sha512'): Promise<ArrayBuffer> {
if (this.isEdge) { if (this.isEdge) {
const valueBytes = this.toForgeBytes(value); const valueBytes = this.toByteString(value);
const keyBytes = this.toForgeBytes(key); const keyBytes = this.toByteString(key);
const hmac = (forge as any).hmac.create(); const hmac = (forge as any).hmac.create();
hmac.start(algorithm, keyBytes); hmac.start(algorithm, keyBytes);
hmac.update(valueBytes); hmac.update(valueBytes);
return this.fromForgeBytesToBuf(hmac.digest().getBytes()); return this.fromByteStringToBuf(hmac.digest().getBytes());
} }
const signingAlgorithm = { const signingAlgorithm = {
@ -90,20 +90,22 @@ export class WebCryptoFunctionService implements CryptoFunctionService {
return buf; return buf;
} }
private toForgeBytes(value: string | ArrayBuffer): string { private toByteString(value: string | ArrayBuffer): string {
let bytes: string; let bytes: string;
if (typeof (value) === 'string') { if (typeof (value) === 'string') {
bytes = forge.util.encodeUtf8(value); bytes = forge.util.encodeUtf8(value);
} else { } else {
const value64 = UtilsService.fromBufferToB64(value); bytes = String.fromCharCode.apply(null, new Uint8Array(value));
bytes = forge.util.decode64(value64);
} }
return bytes; return bytes;
} }
private fromForgeBytesToBuf(byteString: string): ArrayBuffer { private fromByteStringToBuf(byteString: string): ArrayBuffer {
const b64 = forge.util.encode64(byteString); const arr = new Uint8Array(byteString.length);
return UtilsService.fromB64ToArray(b64).buffer; for (let i = 0; i < byteString.length; i++) {
arr[i] = byteString.charCodeAt(i);
}
return arr.buffer;
} }
private toWebCryptoAlgorithm(algorithm: 'sha1' | 'sha256' | 'sha512'): string { private toWebCryptoAlgorithm(algorithm: 'sha1' | 'sha256' | 'sha512'): string {