• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.base64URLUnescape = exports.base64URLEscape = exports.base64URLDecode = exports.base64URLEncode = exports.base64Decode = exports.base64Encode = void 0;
4/*
5Copyright 2022 The Sigstore Authors.
6
7Licensed under the Apache License, Version 2.0 (the "License");
8you may not use this file except in compliance with the License.
9You may obtain a copy of the License at
10
11    http://www.apache.org/licenses/LICENSE-2.0
12
13Unless required by applicable law or agreed to in writing, software
14distributed under the License is distributed on an "AS IS" BASIS,
15WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16See the License for the specific language governing permissions and
17limitations under the License.
18*/
19const BASE64_ENCODING = 'base64';
20const UTF8_ENCODING = 'utf-8';
21function base64Encode(str) {
22    return Buffer.from(str, UTF8_ENCODING).toString(BASE64_ENCODING);
23}
24exports.base64Encode = base64Encode;
25function base64Decode(str) {
26    return Buffer.from(str, BASE64_ENCODING).toString(UTF8_ENCODING);
27}
28exports.base64Decode = base64Decode;
29function base64URLEncode(str) {
30    return base64URLEscape(base64Encode(str));
31}
32exports.base64URLEncode = base64URLEncode;
33function base64URLDecode(str) {
34    return base64Decode(base64URLUnescape(str));
35}
36exports.base64URLDecode = base64URLDecode;
37function base64URLEscape(str) {
38    return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
39}
40exports.base64URLEscape = base64URLEscape;
41function base64URLUnescape(str) {
42    // Repad the base64 string if necessary
43    str += '='.repeat((4 - (str.length % 4)) % 4);
44    return str.replace(/-/g, '+').replace(/_/g, '/');
45}
46exports.base64URLUnescape = base64URLUnescape;
47