UNPKG

@ar.io/sdk

Version:

[![codecov](https://codecov.io/gh/ar-io/ar-io-sdk/graph/badge.svg?token=7dXKcT7dJy)](https://codecov.io/gh/ar-io/ar-io-sdk)

63 lines (62 loc) 2.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromB64Url = fromB64Url; exports.toB64Url = toB64Url; exports.sha256B64Url = sha256B64Url; exports.getRandomText = getRandomText; /** * Copyright (C) 2022-2024 Permanent Data Solutions, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const crypto_1 = require("crypto"); // safely encodes and decodes base64url strings to and from buffers const BASE64_CHAR_62 = '+'; const BASE64_CHAR_63 = '/'; const BASE64URL_CHAR_62 = '-'; const BASE64URL_CHAR_63 = '_'; const BASE64_PADDING = '='; function base64urlToBase64(str) { const padLength = str.length % 4; if (padLength) { str += BASE64_PADDING.repeat(4 - padLength); } return str .replaceAll(BASE64URL_CHAR_62, BASE64_CHAR_62) .replaceAll(BASE64URL_CHAR_63, BASE64_CHAR_63); } function base64urlFromBase64(str) { return str .replaceAll(BASE64_CHAR_62, BASE64URL_CHAR_62) .replaceAll(BASE64_CHAR_63, BASE64URL_CHAR_63) .replaceAll(BASE64_PADDING, ''); } function fromB64Url(str) { const b64Str = base64urlToBase64(str); return Buffer.from(b64Str, 'base64'); } function toB64Url(buffer) { const b64Str = buffer.toString('base64'); return base64urlFromBase64(b64Str); } function sha256B64Url(input) { return toB64Url((0, crypto_1.createHash)('sha256').update(Uint8Array.from(input)).digest()); } function getRandomText(length = 32) { // Generate a buffer of random bytes const buffer = (0, crypto_1.randomBytes)(length); // Convert bytes to hexadecimal string return Array.from(buffer, (byte) => byte.toString(16).padStart(2, '0')) .join('') .slice(0, length); }