UNPKG

@turnkey/api-key-stamper

Version:

API key stamper for @turnkey/http

82 lines (79 loc) 2.12 kB
'use strict'; /** * Code modified from https://github.com/google/tink/blob/6f74b99a2bfe6677e3670799116a57268fd067fa/javascript/subtle/bytes.ts * * @license * Copyright 2020 Google LLC * SPDX-License-Identifier: Apache-2.0 */ /** * Converts the hex string to a byte array. * * @param hex the input * @return the byte array output * @throws {!Error} * @static */ function fromHex(hex) { if (hex.length % 2 != 0) { throw new Error("Hex string length must be multiple of 2"); } const arr = new Uint8Array(hex.length / 2); for (let i = 0; i < hex.length; i += 2) { arr[i / 2] = parseInt(hex.substring(i, i + 2), 16); } return arr; } /** * Converts a byte array to hex. * * @param bytes the byte array input * @return hex the output * @static */ function toHex(bytes) { let result = ""; for (let i = 0; i < bytes.length; i++) { const hexByte = bytes[i].toString(16); result += hexByte.length > 1 ? hexByte : "0" + hexByte; } return result; } /** * Base64 encode a byte array. * * @param bytes the byte array input * @param opt_webSafe True indicates we should use the alternative * alphabet, which does not require escaping for use in URLs. * @return base64 output * @static */ function toBase64(bytes, opt_webSafe) { const encoded = btoa( /* padding */ toByteString(bytes)).replace(/=/g, ""); { return encoded.replace(/\+/g, "-").replace(/\//g, "_"); } } /** * Turns a byte array into the string given by the concatenation of the * characters to which the numbers correspond. Each byte is corresponding to a * character. Does not support multi-byte characters. * * @param bytes Array of numbers representing * characters. * @return Stringification of the array. */ function toByteString(bytes) { let str = ""; for (let i = 0; i < bytes.length; i += 1) { str += String.fromCharCode(bytes[i]); } return str; } exports.fromHex = fromHex; exports.toBase64 = toBase64; exports.toByteString = toByteString; exports.toHex = toHex; //# sourceMappingURL=bytes.js.map