UNPKG

@safeheron/crypto-utils

Version:
50 lines 1.96 kB
"use strict"; /** * Convert binary data to and from UrlBase64 encoding. * This is identical to Base64 encoding, except that the padding character is "." and the other * non-alphanumeric characters are "-" and "_" instead of "+" and "/". * The purpose of UrlBase64 encoding is to provide a compact encoding of binary data that is safe * for use as an URL parameter. Base64 encoding does not produce encoded values that are safe for * use in URLs, since "/" can be interpreted as a path delimiter; "+" is the encoded form of a space; * and "=" is used to separate a name from the corresponding value in an URL parameter. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.UrlBase64 = void 0; const CryptoJS = require("crypto-js"); const hex_1 = require("./hex"); class UrlBase64 { /** * UrlBase64 ===> Bytes: * @param urlBase64 */ static toBytes(urlBase64) { // UrlBase64 ==> CryptoJSBytes ==> Hex ==> Bytes: Uint8Array return hex_1.Hex.toBytes(hex_1.Hex.fromCryptoJSBytes(UrlBase64.toCryptoJSBytes(urlBase64))); } /** * Bytes(Uint8Array) ===> UrlBase64: * @param bytes */ static fromBytes(bytes) { // Bytes: Uint8Array ==> Hex ==> CryptoJSBytes ==> UrlBase64 return UrlBase64.fromCryptoJSBytes(hex_1.Hex.toCryptoJSBytes(hex_1.Hex.fromBytes(bytes))); } /** * UrlBase64 ===> CryptoJSBytes: * @param urlBase64 */ static toCryptoJSBytes(urlBase64) { let base64 = urlBase64.replace(/\./g, '=').replace(/-/g, '+').replace(/_/g, '/'); return CryptoJS.enc.Base64.parse(base64); } /** * CryptoJSBytes ===> UrlBase64: * @param bytes */ static fromCryptoJSBytes(bytes) { let base64 = CryptoJS.enc.Base64.stringify(bytes); return base64.replace(/=/g, '.').replace(/\+/g, '-').replace(/\//g, '_'); } } exports.UrlBase64 = UrlBase64; //# sourceMappingURL=urlBase64.js.map