Skip to content

Commit aac9eb1

Browse files
committed
Removing buffer.
1 parent d90df34 commit aac9eb1

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

examples/partially_blindrsa.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
// Copyright (c) 2024 Cloudflare, Inc.
22
// Licensed under the Apache-2.0 license found in the LICENSE file or at https://opensource.org/licenses/Apache-2.0
33
import sjcl from 'sjcl';
4-
import { Buffer } from 'node:buffer';
54
import { generatePrimeSync } from 'node:crypto';
65

76
import type { PartiallyBlindRSA } from '../src/index.js';
87

9-
function hexNumToB64URL(x: string): string {
8+
export function hexToUint8(x: string): Uint8Array {
109
if (x.startsWith('0x')) {
11-
x = x.slice(2);
10+
x = x.substring(2);
1211
}
13-
return Buffer.from(x, 'hex').toString('base64url');
12+
return Uint8Array.from({ length: Math.floor(x.length / 2) }, (_, i) =>
13+
parseInt(x.substring(2 * i, 2 * i + 2), 16),
14+
);
15+
}
16+
17+
function hexNumToB64URL(x: string): string {
18+
const s = btoa(String.fromCharCode(...hexToUint8(x)));
19+
return s.replace(/\+/g, '-').replace(/\//g, '_');
1420
}
1521

1622
async function preGeneratedKeys(extractable = true): Promise<CryptoKeyPair> {

test/conv.ts

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
import { Buffer } from 'node:buffer';
2-
3-
export function hexNumToB64URL(x: string): string {
4-
if (x.startsWith('0x')) {
5-
x = x.slice(2);
6-
}
7-
return Buffer.from(x, 'hex').toString('base64url');
8-
}
9-
101
export function hexToUint8(x: string): Uint8Array {
112
if (x.startsWith('0x')) {
12-
x = x.slice(2);
3+
x = x.substring(2);
134
}
14-
return new Uint8Array(Buffer.from(x, 'hex'));
5+
return Uint8Array.from({ length: Math.floor(x.length / 2) }, (_, i) =>
6+
parseInt(x.substring(2 * i, 2 * i + 2), 16),
7+
);
158
}
169

1710
export function uint8ToHex(x: Uint8Array): string {
18-
return Buffer.from(x).toString('hex');
11+
return Array.from(x)
12+
.map((b) => b.toString(16).padStart(2, '0'))
13+
.join('');
14+
}
15+
16+
function b64ToB64URL(s: string) {
17+
return s.replace(/\+/g, '-').replace(/\//g, '_');
18+
}
19+
20+
export function hexNumToB64URL(x: string): string {
21+
return b64ToB64URL(btoa(String.fromCharCode(...hexToUint8(x))));
1922
}

0 commit comments

Comments
 (0)