Skip to content

Commit 34ccc91

Browse files
authored
fix(cname-prefix): use bigint instead of number (#545)
Co-authored-by: nd0ut <[email protected]>
1 parent 170cb0a commit 34ccc91

File tree

8 files changed

+28
-20
lines changed

8 files changed

+28
-20
lines changed

packages/cname-prefix/src/async/getCnamePrefix.test.ts renamed to packages/cname-prefix/src/async/getCnamePrefixAsync.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ describe('getCnamePrefixAsync', () => {
55
it('should generate a CNAME prefix from a public key', async () => {
66
expect(await getCnamePrefixAsync('demopublickey')).toBe('1s4oyld5dc')
77
expect(await getCnamePrefixAsync('c8c237984266090ff9b8')).toBe('127mbvwq3b')
8+
expect(await getCnamePrefixAsync('3e6ba70c0670de3bef7a')).toBe('u51bthcx6t')
9+
expect(await getCnamePrefixAsync('823a5ae6eb3afa5b353f')).toBe('ggiwfssv31')
810
})
911
})

packages/cname-prefix/src/async/sha256EncodeAsync.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { describe, it, expect } from 'vitest'
33

44
describe('sha256EncodeAsync', () => {
55
it('should encode a string to a SHA-256 hash and return it as an base 16 integer', async () => {
6-
expect(await sha256EncodeAsync('demopublickey')).toBe(3.2328368644851214e76)
6+
expect(await sha256EncodeAsync('demopublickey')).toBe(
7+
32328368644851216602162954773669835699253099229990596035124766862276663471280n
8+
)
79
})
810
})
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
export const sha256EncodeAsync = async (data: string): Promise<number> => {
1+
export const sha256EncodeAsync = async (data: string): Promise<bigint> => {
22
const msgUint8 = new TextEncoder().encode(data)
33
const hashBuffer = await window.crypto.subtle.digest('SHA-256', msgUint8)
44
const hashArray = Array.from(new Uint8Array(hashBuffer))
55
const hashHexStr = hashArray
66
.map((b) => b.toString(16).padStart(2, '0'))
77
.join('')
8-
const hashHexInt = parseInt(hashHexStr, 16)
8+
const hashHexInt = BigInt(`0x${hashHexStr}`)
99
return hashHexInt
1010
}

packages/cname-prefix/src/common/base36Encode.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { describe, it, expect } from 'vitest'
33

44
describe('base36Encode', () => {
55
it('should encode a number to base36', () => {
6-
expect(base36Encode(0)).toBe('0')
7-
expect(base36Encode(1)).toBe('1')
8-
expect(base36Encode(35)).toBe('z')
9-
expect(base36Encode(36)).toBe('10')
10-
expect(base36Encode(123456789)).toBe('21i3v9')
11-
expect(base36Encode(987654321)).toBe('gc0uy9')
12-
expect(base36Encode(1234567890)).toBe('kf12oi')
13-
expect(base36Encode(9876543210)).toBe('4jc8lii')
6+
expect(base36Encode(0n)).toBe('0')
7+
expect(base36Encode(1n)).toBe('1')
8+
expect(base36Encode(35n)).toBe('z')
9+
expect(base36Encode(36n)).toBe('10')
10+
expect(base36Encode(123456789n)).toBe('21i3v9')
11+
expect(base36Encode(987654321n)).toBe('gc0uy9')
12+
expect(base36Encode(1234567890n)).toBe('kf12oi')
13+
expect(base36Encode(9876543210n)).toBe('4jc8lii')
1414
})
1515
})
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz'
22

3-
export const base36Encode = (value: number): string => {
4-
if (value <= 0) {
3+
export const base36Encode = (value: bigint): string => {
4+
if (value <= 0n) {
55
return '0'
66
}
77
let result = ''
8-
while (value > 0) {
9-
const remainder = value % 36
10-
result = ALPHABET[remainder] + result
11-
value = Math.floor(value / 36)
8+
while (value > 0n) {
9+
const remainder = value % 36n
10+
result = ALPHABET[Number(remainder)] + result
11+
value = value / 36n
1212
}
1313
return result
1414
}

packages/cname-prefix/src/sync/getCnamePrefixSync.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ describe('getCnamePrefixSync', () => {
55
it('should generate a CNAME prefix from a public key', () => {
66
expect(getCnamePrefixSync('demopublickey')).toBe('1s4oyld5dc')
77
expect(getCnamePrefixSync('c8c237984266090ff9b8')).toBe('127mbvwq3b')
8+
expect(getCnamePrefixSync('3e6ba70c0670de3bef7a')).toBe('u51bthcx6t')
9+
expect(getCnamePrefixSync('823a5ae6eb3afa5b353f')).toBe('ggiwfssv31')
810
})
911
})

packages/cname-prefix/src/sync/sha256EncodeSync.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { describe, it, expect } from 'vitest'
33

44
describe('sha256EncodeSync', () => {
55
it('should encode a string to a SHA-256 hash and return it as an base 16 integer', async () => {
6-
expect(sha256EncodeSync('demopublickey')).toBe(3.2328368644851214e76)
6+
expect(sha256EncodeSync('demopublickey')).toBe(
7+
32328368644851216602162954773669835699253099229990596035124766862276663471280n
8+
)
79
})
810
})
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Sha256 } from './Sha256'
22

3-
export function sha256EncodeSync(message: string): number {
3+
export function sha256EncodeSync(message: string): bigint {
44
const hash = new Sha256()
55
hash.update(message)
66
const hashHexStr = hash.hex()
7-
const hashHexInt = parseInt(hashHexStr, 16)
7+
const hashHexInt = BigInt(`0x${hashHexStr}`)
88
return hashHexInt
99
}

0 commit comments

Comments
 (0)