UNPKG

@bicycle-codes/simple-aes

Version:

An easy way to use symmetric keys in browsers or node

36 lines 1.31 kB
import { fromString } from 'uint8arrays'; var CharSize; (function (CharSize) { CharSize[CharSize["B8"] = 8] = "B8"; CharSize[CharSize["B16"] = 16] = "B16"; })(CharSize || (CharSize = {})); export const normalizeToBuf = function normalizeToBuf(msg, strConv) { if (typeof msg === 'string') { return strConv(msg); } else if (typeof msg === 'object' && msg.byteLength !== undefined) { // this is the best runtime check I could find for ArrayBuffer/Uint8Array const temp = new Uint8Array(msg); return temp.buffer; } else { throw new Error('Improper value. Must be a string, ArrayBuffer, Uint8Array'); } }; export function base64ToArrBuf(encoding, string) { return fromString(string, encoding).buffer; } export function normalizeBase64ToBuf(msg, encoding) { return normalizeToBuf(msg, base64ToArrBuf.bind(null, encoding)); } export const normalizeUtf16ToBuf = (msg) => { return normalizeToBuf(msg, (str) => strToArrBuf(str, CharSize.B16)); }; function strToArrBuf(str, charSize) { const view = charSize === 8 ? new Uint8Array(str.length) : new Uint16Array(str.length); for (let i = 0, strLen = str.length; i < strLen; i++) { view[i] = str.charCodeAt(i); } return view.buffer; } //# sourceMappingURL=util.js.map