Skip to content

Commit 9d155dd

Browse files
committed
chore: wip
1 parent 604eb28 commit 9d155dd

20 files changed

Lines changed: 1739 additions & 29 deletions

bun.lock

Lines changed: 47 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"name": "ts-security",
33
"type": "module",
4+
"workspaces": [
5+
"packages/*"
6+
],
47
"version": "0.0.0",
58
"description": "A TLS/HTTPS library with automation.",
69
"author": "Chris Breuer <chris@stacksjs.org> (https://github.com/chrisbbreuer)",

packages/base-x/package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@
5353
"test": "bun test",
5454
"typecheck": "bun --bun tsc --noEmit"
5555
},
56-
"devDependencies": {
57-
"@types/bun": "^1.2.3",
58-
"bun-plugin-dtsx": "^0.21.9",
59-
"typescript": "^5.7.3"
60-
},
6156
"overrides": {
6257
"unconfig": "0.3.10"
6358
},

packages/base-x/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// base-x encoding / decoding
22
// Copyright (c) 2018 base-x contributors
33
// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
4-
// Distributed under the MIT software license, see the accompanying
5-
// @see http://www.opensource.org/licenses/mit-license.php
4+
// Distributed under the MIT software license, see the accompanying:
5+
// http://www.opensource.org/licenses/mit-license.php
66

77
// Common alphabets and their bases
88
export const ALPHABETS = {
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { expect, it, describe } from 'bun:test'
2+
import { base, ALPHABETS, base64, base58, base32, base16, base2 } from '../src'
3+
4+
describe('ts-base-x', () => {
5+
describe('base function configuration', () => {
6+
it('should create a base converter with default BASE58 alphabet', () => {
7+
const converter = base()
8+
const input = new Uint8Array([72, 101, 108, 108, 111]) // "Hello"
9+
const encoded = converter.encode(input)
10+
const decoded = converter.decode(encoded)
11+
expect(decoded).toEqual(input)
12+
})
13+
14+
it('should throw error for alphabet too long', () => {
15+
const longAlphabet = 'x'.repeat(255)
16+
expect(() => base(longAlphabet)).toThrow('Alphabet too long')
17+
})
18+
19+
it('should throw error for ambiguous alphabet', () => {
20+
expect(() => base('00123')).toThrow('0 is ambiguous')
21+
})
22+
})
23+
24+
describe('encoding', () => {
25+
it('should encode Uint8Array input', () => {
26+
const input = new Uint8Array([1, 2, 3])
27+
const encoded = base64.encode(input)
28+
expect(typeof encoded).toBe('string')
29+
expect(encoded).toBe('QID')
30+
})
31+
32+
it('should handle empty input', () => {
33+
const input = new Uint8Array(0)
34+
const encoded = base64.encode(input)
35+
expect(encoded).toBe('')
36+
})
37+
38+
it('should handle leading zeros', () => {
39+
const input = new Uint8Array([0, 0, 1])
40+
const encoded = base58.encode(input)
41+
expect(encoded.startsWith('11')).toBe(true)
42+
})
43+
})
44+
45+
describe('decoding', () => {
46+
it('should decode valid base64 string', () => {
47+
const input = 'QID'
48+
const decoded = base64.decode(input)
49+
expect(decoded).toEqual(new Uint8Array([1, 2, 3]))
50+
})
51+
52+
it('should handle empty string', () => {
53+
const input = ''
54+
const decoded = base64.decode(input)
55+
expect(decoded).toEqual(new Uint8Array(0))
56+
})
57+
58+
it('should throw error for invalid characters', () => {
59+
expect(() => base58.decode('invalid!')).toThrow('Non-base58 character')
60+
})
61+
})
62+
63+
describe('different bases', () => {
64+
it('should work with base16 (hex)', () => {
65+
const input = new Uint8Array([255, 0, 128])
66+
const encoded = base16.encode(input)
67+
expect(encoded).toBe('ff0080')
68+
expect(base16.decode(encoded)).toEqual(input)
69+
})
70+
71+
it('should work with base2 (binary)', () => {
72+
const input = new Uint8Array([5]) // 00000101 in binary
73+
const encoded = base2.encode(input)
74+
expect(encoded).toBe('101')
75+
expect(base2.decode(encoded)).toEqual(input)
76+
})
77+
78+
it('should work with base32', () => {
79+
const input = new Uint8Array([72, 101, 108, 108, 111]) // "Hello"
80+
const encoded = base32.encode(input)
81+
const decoded = base32.decode(encoded)
82+
expect(decoded).toEqual(input)
83+
})
84+
})
85+
86+
describe('error handling', () => {
87+
it('should throw for invalid input types in encode', () => {
88+
expect(() => base64.encode('not a Uint8Array' as any)).toThrow('Expected Uint8Array')
89+
})
90+
91+
it('should throw for invalid input types in decode', () => {
92+
expect(() => base64.decode(123 as any)).toThrow('Expected String')
93+
})
94+
95+
it('should throw for invalid characters', () => {
96+
// The implementation uses 'Non-base58 character' for all bases
97+
expect(() => base58.decode('invalid!')).toThrow('Non-base58 character')
98+
expect(() => base16.decode('0123456789abcdefg')).toThrow('Non-base58 character')
99+
expect(() => base2.decode('1012')).toThrow('Non-base58 character')
100+
})
101+
})
102+
103+
describe('roundtrip tests', () => {
104+
const testCases = [
105+
new Uint8Array([]),
106+
new Uint8Array([0]),
107+
new Uint8Array([1, 2, 3, 4, 5]),
108+
new Uint8Array([0, 0, 0, 0, 1]),
109+
new Uint8Array([255, 255, 255]),
110+
new Uint8Array(Array(100).fill(65)), // Longer input
111+
]
112+
113+
for (const testCase of testCases) {
114+
it(`should correctly roundtrip ${testCase.length} bytes`, () => {
115+
const encoded = base64.encode(testCase)
116+
const decoded = base64.decode(encoded)
117+
expect(decoded).toEqual(testCase)
118+
})
119+
}
120+
})
121+
})

packages/base-x/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../tsconfig.json"
3+
}

packages/sha/package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,8 @@
4848
"test": "bun test",
4949
"typecheck": "bun --bun tsc --noEmit"
5050
},
51-
"devDependencies": {
52-
"@stacksjs/docs": "^0.69.3",
53-
"@types/bun": "^1.2.3",
54-
"bun-plugin-dtsx": "^0.21.9",
55-
"typescript": "^5.7.3"
51+
"dependencies": {
52+
"ts-security-utils": "workspace:*"
5653
},
5754
"overrides": {
5855
"unconfig": "0.3.10"

packages/sha/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './sha1'
2+
export * from './sha256'
3+
export * from './sha384'
4+
export * from './sha512'

0 commit comments

Comments
 (0)