UNPKG

1.6 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.hexToBase64 = void 0;
4/*
5 * Copyright The OpenTelemetry Authors
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * https://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19function intValue(charCode) {
20 // 0-9
21 if (charCode >= 48 && charCode <= 57) {
22 return charCode - 48;
23 }
24 // a-f
25 if (charCode >= 97 && charCode <= 102) {
26 return charCode - 87;
27 }
28 // A-F
29 return charCode - 55;
30}
31const buf8 = Buffer.alloc(8);
32const buf16 = Buffer.alloc(16);
33function hexToBase64(hexStr) {
34 let buf;
35 if (hexStr.length === 16) {
36 buf = buf8;
37 }
38 else if (hexStr.length === 32) {
39 buf = buf16;
40 }
41 else {
42 buf = Buffer.alloc(hexStr.length / 2);
43 }
44 let offset = 0;
45 for (let i = 0; i < hexStr.length; i += 2) {
46 const hi = intValue(hexStr.charCodeAt(i));
47 const lo = intValue(hexStr.charCodeAt(i + 1));
48 buf.writeUInt8((hi << 4) | lo, offset++);
49 }
50 return buf.toString('base64');
51}
52exports.hexToBase64 = hexToBase64;
53//# sourceMappingURL=hex-to-base64.js.map
\No newline at end of file