mirror of
https://github.com/bitwarden/browser
synced 2025-12-20 10:13:31 +00:00
misc utils for browser and node
This commit is contained in:
76
src/misc/utils.ts
Normal file
76
src/misc/utils.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
export class Utils {
|
||||
static inited = false;
|
||||
static isNode = false;
|
||||
static isBrowser = true;
|
||||
|
||||
static init() {
|
||||
if (Utils.inited) {
|
||||
return;
|
||||
}
|
||||
|
||||
Utils.inited = true;
|
||||
Utils.isNode = typeof window === 'undefined';
|
||||
Utils.isBrowser = !Utils.isNode;
|
||||
}
|
||||
|
||||
static fromB64ToArray(str: string): Uint8Array {
|
||||
if (Utils.isNode) {
|
||||
return new Uint8Array(Buffer.from(str, 'base64'));
|
||||
} else {
|
||||
const binaryString = window.atob(str);
|
||||
const bytes = new Uint8Array(binaryString.length);
|
||||
for (let i = 0; i < binaryString.length; i++) {
|
||||
bytes[i] = binaryString.charCodeAt(i);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
|
||||
static fromUtf8ToArray(str: string): Uint8Array {
|
||||
if (Utils.isNode) {
|
||||
return new Uint8Array(Buffer.from(str, 'utf8'));
|
||||
} else {
|
||||
const strUtf8 = unescape(encodeURIComponent(str));
|
||||
const arr = new Uint8Array(strUtf8.length);
|
||||
for (let i = 0; i < strUtf8.length; i++) {
|
||||
arr[i] = strUtf8.charCodeAt(i);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
static fromBufferToB64(buffer: ArrayBuffer): string {
|
||||
if (Utils.isNode) {
|
||||
return new Buffer(buffer).toString('base64');
|
||||
} else {
|
||||
let binary = '';
|
||||
const bytes = new Uint8Array(buffer);
|
||||
for (let i = 0; i < bytes.byteLength; i++) {
|
||||
binary += String.fromCharCode(bytes[i]);
|
||||
}
|
||||
return window.btoa(binary);
|
||||
}
|
||||
}
|
||||
|
||||
static fromBufferToUtf8(buffer: ArrayBuffer): string {
|
||||
if (Utils.isNode) {
|
||||
return new Buffer(buffer).toString('utf8');
|
||||
} else {
|
||||
const bytes = new Uint8Array(buffer);
|
||||
const encodedString = String.fromCharCode.apply(null, bytes);
|
||||
return decodeURIComponent(escape(encodedString));
|
||||
}
|
||||
}
|
||||
|
||||
// ref: https://stackoverflow.com/a/40031979/1090359
|
||||
static fromBufferToHex(buffer: ArrayBuffer): string {
|
||||
if (Utils.isNode) {
|
||||
return new Buffer(buffer).toString('hex');
|
||||
} else {
|
||||
const bytes = new Uint8Array(buffer);
|
||||
return Array.prototype.map.call(bytes, (x: number) => ('00' + x.toString(16)).slice(-2)).join('');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Utils.init();
|
||||
@@ -14,7 +14,7 @@ export class NodeCryptoFunctionService implements CryptoFunctionService {
|
||||
if (error != null) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(key.buffer);
|
||||
resolve(this.toArrayBuffer(key));
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -24,7 +24,7 @@ export class NodeCryptoFunctionService implements CryptoFunctionService {
|
||||
const nodeValue = this.toNodeValue(value);
|
||||
const hash = crypto.createHash(algorithm);
|
||||
hash.update(nodeValue);
|
||||
return Promise.resolve(hash.digest().buffer);
|
||||
return Promise.resolve(this.toArrayBuffer(hash.digest()));
|
||||
}
|
||||
|
||||
hmac(value: ArrayBuffer, key: ArrayBuffer, algorithm: 'sha1' | 'sha256' | 'sha512'): Promise<ArrayBuffer> {
|
||||
@@ -32,7 +32,7 @@ export class NodeCryptoFunctionService implements CryptoFunctionService {
|
||||
const nodeKey = this.toNodeBuffer(value);
|
||||
const hmac = crypto.createHmac(algorithm, nodeKey);
|
||||
hmac.update(nodeValue);
|
||||
return Promise.resolve(hmac.digest().buffer);
|
||||
return Promise.resolve(this.toArrayBuffer(hmac.digest()));
|
||||
}
|
||||
|
||||
aesEncrypt(data: ArrayBuffer, iv: ArrayBuffer, key: ArrayBuffer): Promise<ArrayBuffer> {
|
||||
@@ -41,7 +41,7 @@ export class NodeCryptoFunctionService implements CryptoFunctionService {
|
||||
const nodeKey = this.toNodeBuffer(key);
|
||||
const cipher = crypto.createCipheriv('aes-256-cbc', nodeKey, nodeIv);
|
||||
const encBuf = Buffer.concat([cipher.update(nodeData), cipher.final()]);
|
||||
return Promise.resolve(encBuf.buffer);
|
||||
return Promise.resolve(this.toArrayBuffer(encBuf));
|
||||
}
|
||||
|
||||
aesDecryptSmall(data: ArrayBuffer, iv: ArrayBuffer, key: ArrayBuffer): Promise<ArrayBuffer> {
|
||||
@@ -54,7 +54,7 @@ export class NodeCryptoFunctionService implements CryptoFunctionService {
|
||||
const nodeKey = this.toNodeBuffer(key);
|
||||
const decipher = crypto.createDecipheriv('aes-256-cbc', nodeKey, nodeIv);
|
||||
const decBuf = Buffer.concat([decipher.update(nodeData), decipher.final()]);
|
||||
return Promise.resolve(decBuf.buffer);
|
||||
return Promise.resolve(this.toArrayBuffer(decBuf));
|
||||
}
|
||||
|
||||
rsaDecrypt(data: ArrayBuffer, key: ArrayBuffer, algorithm: 'sha1' | 'sha256'): Promise<ArrayBuffer> {
|
||||
@@ -68,7 +68,7 @@ export class NodeCryptoFunctionService implements CryptoFunctionService {
|
||||
padding: constants.RSA_PKCS1_OAEP_PADDING,
|
||||
};
|
||||
const decBuf = crypto.publicDecrypt(rsaKey, nodeData);
|
||||
return Promise.resolve(decBuf.buffer);
|
||||
return Promise.resolve(this.toArrayBuffer(decBuf));
|
||||
}
|
||||
|
||||
randomBytes(length: number): Promise<ArrayBuffer> {
|
||||
@@ -77,7 +77,7 @@ export class NodeCryptoFunctionService implements CryptoFunctionService {
|
||||
if (error != null) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(bytes.buffer);
|
||||
resolve(this.toArrayBuffer(bytes));
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -97,6 +97,10 @@ export class NodeCryptoFunctionService implements CryptoFunctionService {
|
||||
return Buffer.from(new Uint8Array(value) as any);
|
||||
}
|
||||
|
||||
private toArrayBuffer(buf: Buffer): ArrayBuffer {
|
||||
return new Uint8Array(buf).buffer;
|
||||
}
|
||||
|
||||
private toPem(key: ArrayBuffer): string {
|
||||
const b64Key = ''; // TODO: key to b84
|
||||
return '-----BEGIN PRIVATE KEY-----\n' + b64Key + '\n-----END PRIVATE KEY-----';
|
||||
|
||||
@@ -3,7 +3,7 @@ import * as forge from 'node-forge';
|
||||
import { CryptoFunctionService } from '../abstractions/cryptoFunction.service';
|
||||
import { PlatformUtilsService } from '../abstractions/platformUtils.service';
|
||||
|
||||
import { UtilsService } from '../services/utils.service';
|
||||
import { Utils } from '../misc/utils';
|
||||
|
||||
export class WebCryptoFunctionService implements CryptoFunctionService {
|
||||
private crypto: Crypto;
|
||||
@@ -122,7 +122,7 @@ export class WebCryptoFunctionService implements CryptoFunctionService {
|
||||
private toBuf(value: string | ArrayBuffer): ArrayBuffer {
|
||||
let buf: ArrayBuffer;
|
||||
if (typeof (value) === 'string') {
|
||||
buf = UtilsService.fromUtf8ToArray(value).buffer;
|
||||
buf = Utils.fromUtf8ToArray(value).buffer;
|
||||
} else {
|
||||
buf = value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user