Skip to content

Commit 4d5b9f6

Browse files
committed
chore: wip
1 parent c57aba4 commit 4d5b9f6

10 files changed

Lines changed: 550 additions & 20 deletions

File tree

packages/base-x/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@
5353
"test": "bun test",
5454
"typecheck": "bun --bun tsc --noEmit"
5555
},
56-
"overrides": {
57-
"unconfig": "0.3.10"
58-
},
5956
"simple-git-hooks": {
6057
"pre-commit": "bun lint-staged"
6158
},

packages/sha/README.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<p align="center"><img src="../../.github/art/cover.jpg" alt="Social Card of this repo"></p>
2+
3+
[![npm version][npm-version-src]][npm-version-href]
4+
[![GitHub Actions][github-actions-src]][github-actions-href]
5+
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
6+
<!-- [![npm downloads][npm-downloads-src]][npm-downloads-href] -->
7+
<!-- [![Codecov][codecov-src]][codecov-href] -->
8+
9+
# ts-sha
10+
11+
> A comprehensive TypeScript security library providing cryptographic primitives and utilities with a focus on type safety, security, performance, and modern best practices.
12+
13+
## Features
14+
15+
- 🔒 **Cryptographic Primitives**
16+
- AES encryption _(128/192/256-bit)_ with multiple modes _(ECB, CBC, CFB, OFB, CTR, GCM)_
17+
- SHA-2 family hash functions _(SHA-256, SHA-384, SHA-512)_
18+
- HMAC message authentication
19+
- RSA encryption and signing
20+
- Ed25519 digital signatures
21+
22+
- 🛡️ **Secure Random Number Generation**
23+
- Fortuna CSPRNG implementation
24+
- Multiple entropy sources
25+
- Automatic reseeding
26+
- Browser and Bun / Node.js support
27+
28+
- 📜 **Certificate Management**
29+
- X.509 certificate handling
30+
- PEM encoding/decoding
31+
- Certificate signing request _(CSR)_ creation
32+
- Certificate chain validation
33+
34+
- 🔐 **TLS/SSL Support**
35+
- TLS protocol implementation
36+
- Secure socket connections
37+
- Certificate-based authentication
38+
- Modern cipher suite support
39+
40+
- 🎯 **Type Safety**
41+
- Full TypeScript support
42+
- Comprehensive type definitions
43+
- Strict type checking
44+
- Modern ES6+ features
45+
46+
- 🧰 **Utilities**
47+
- Base-N encoding _(Base64, Base58, etc.)_
48+
- ASN.1 encoding/decoding
49+
- BigInteger arithmetic
50+
- Buffer manipulation
51+
52+
## Install
53+
54+
```bash
55+
# bun
56+
bun install ts-security
57+
58+
# npm
59+
npm install ts-security
60+
61+
# pnpm
62+
pnpm install ts-security
63+
```
64+
65+
## Get Started
66+
67+
After installing the package, you can import and use the various cryptographic functions:
68+
69+
```ts
70+
import {
71+
aes,
72+
ed25519,
73+
hmac,
74+
pki,
75+
random,
76+
rsa,
77+
sha256,
78+
sha512,
79+
tls
80+
} from 'ts-security'
81+
82+
// AES Encryption
83+
const cipher = aes.createCipher('AES-GCM', key)
84+
cipher.start({ iv })
85+
cipher.update(data)
86+
const encrypted = cipher.finish()
87+
88+
// SHA-256 Hashing
89+
const md = sha256.create()
90+
md.update('Hello, World!')
91+
const hash = md.digest().toHex()
92+
93+
// Secure Random Numbers
94+
const bytes = random.getBytesSync(32)
95+
96+
// RSA Key Generation
97+
const keypair = rsa.generateKeyPair({ bits: 2048 })
98+
99+
// Digital Signatures
100+
const signature = ed25519.sign(message, privateKey)
101+
const isValid = ed25519.verify(signature, message, publicKey)
102+
103+
// Certificate Operations
104+
const cert = pki.createCertificate()
105+
cert.publicKey = keypair.publicKey
106+
cert.sign(keypair.privateKey)
107+
108+
// TLS Connections
109+
const connection = tls.connect({
110+
server: host,
111+
port: 443,
112+
caStore: [/* trusted certificates */]
113+
})
114+
```
115+
116+
For more detailed examples and API documentation, please visit our [documentation](https://ts-security.stacksjs.org).
117+
118+
## Testing
119+
120+
```bash
121+
bun test
122+
```
123+
124+
## Changelog
125+
126+
Please see our [releases](https://github.com/stacksjs/ts-security/releases) page for more information on what has changed recently.
127+
128+
## Contributing
129+
130+
Please review the [Contributing Guide](https://github.com/stacksjs/contributing) for details.
131+
132+
## Community
133+
134+
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
135+
136+
[Discussions on GitHub](https://github.com/stacksjs/stacks/discussions)
137+
138+
For casual chit-chat with others using this package:
139+
140+
[Join the Stacks Discord Server](https://discord.gg/stacksjs)
141+
142+
## Postcardware
143+
144+
“Software that is free, but hopes for a postcard.” We love receiving postcards from around the world showing where `ts-security` is being used! We showcase them on our website too.
145+
146+
Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094, United States 🌎
147+
148+
## Sponsors
149+
150+
We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.
151+
152+
- [JetBrains](https://www.jetbrains.com/)
153+
- [The Solana Foundation](https://solana.com/)
154+
155+
## Credits
156+
157+
- [Dave Longley](https://github.com/dlongley)
158+
- [node-forge](https://github.com/digitalbazaar/forge)
159+
- [Chris Breuer](https://github.com/chrisbbreuer)
160+
- [All Contributors](../../contributors)
161+
162+
## License
163+
164+
The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/stacks/tree/main/LICENSE.md) for more information.
165+
166+
Made with 💙
167+
168+
<!-- Badges -->
169+
[npm-version-src]: https://img.shields.io/npm/v/@stacksjs/ts-security?style=flat-square
170+
[npm-version-href]: https://npmjs.com/package/@stacksjs/ts-security
171+
[github-actions-src]: https://img.shields.io/github/actions/workflow/status/stacksjs/ts-security/ci.yml?style=flat-square&branch=main
172+
[github-actions-href]: https://github.com/stacksjs/ts-security/actions?query=workflow%3Aci
173+
174+
<!-- [codecov-src]: https://img.shields.io/codecov/c/gh/stacksjs/ts-security/main?style=flat-square
175+
[codecov-href]: https://codecov.io/gh/stacksjs/ts-security -->

packages/sha/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@
5151
"dependencies": {
5252
"ts-security-utils": "workspace:*"
5353
},
54-
"overrides": {
55-
"unconfig": "0.3.10"
56-
},
5754
"simple-git-hooks": {
5855
"pre-commit": "bun lint-staged"
5956
},

packages/sha/src/sha256.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
* 4. Supports both synchronous and streaming operations
1313
* 5. Handles UTF-8 encoded input
1414
*
15-
* @author Dave Longley
1615
* @author Chris Breuer
1716
*/
1817

19-
import type { ByteStringBuffer } from '../../utils'
20-
import { createBuffer, fillString } from '../../utils'
18+
import type { ByteStringBuffer } from 'ts-security-utils'
19+
import { createBuffer, fillString } from 'ts-security-utils'
2120

2221
// SHA-256 state interface
2322
interface SHA256State {

packages/sha/test/sha.test.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/sha/test/sha1.test.ts

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { expect, it, describe } from 'bun:test'
2+
import { sha1 } from '../src/sha1'
3+
import { ByteStringBuffer } from 'ts-security-utils'
4+
5+
describe('SHA-1', () => {
6+
describe('API structure', () => {
7+
it('should export a sha1 object with create method', () => {
8+
expect(sha1).toBeDefined()
9+
expect(typeof sha1.create).toBe('function')
10+
})
11+
12+
it('should create a SHA-1 message digest object with correct interface', () => {
13+
const md = sha1.create()
14+
expect(md).toBeDefined()
15+
expect(md.algorithm).toBe('sha1')
16+
expect(md.blockLength).toBe(64)
17+
expect(md.digestLength).toBe(20)
18+
expect(typeof md.start).toBe('function')
19+
expect(typeof md.update).toBe('function')
20+
expect(typeof md.digest).toBe('function')
21+
})
22+
})
23+
24+
describe('hashing functionality', () => {
25+
it('should hash empty string', () => {
26+
const md = sha1.create()
27+
const hash = md.update('').digest()
28+
expect(hash).toBeDefined()
29+
expect(typeof hash.toHex()).toBe('string')
30+
expect(hash.toHex().length).toBe(40) // SHA-1 produces a 160-bit (40 hex chars) hash
31+
})
32+
33+
it('should hash "abc"', () => {
34+
const md = sha1.create()
35+
const hash = md.update('abc').digest()
36+
expect(hash).toBeDefined()
37+
expect(hash.toHex().length).toBe(40)
38+
})
39+
40+
it('should hash longer text', () => {
41+
const md = sha1.create()
42+
const hash = md.update('The quick brown fox jumps over the lazy dog').digest()
43+
expect(hash).toBeDefined()
44+
expect(hash.toHex().length).toBe(40)
45+
})
46+
})
47+
48+
describe('incremental hashing', () => {
49+
it('should support incremental hashing', () => {
50+
const md = sha1.create()
51+
md.update('The quick brown ')
52+
md.update('fox jumps over ')
53+
md.update('the lazy dog')
54+
const hash = md.digest()
55+
expect(hash).toBeDefined()
56+
expect(hash.toHex().length).toBe(40)
57+
})
58+
59+
it('should allow multiple digests from the same instance', () => {
60+
const md = sha1.create()
61+
md.start()
62+
md.update('abc')
63+
const hash1 = md.digest()
64+
65+
md.start()
66+
md.update('def')
67+
const hash2 = md.digest()
68+
69+
expect(hash1).toBeDefined()
70+
expect(hash2).toBeDefined()
71+
// Different inputs should produce different hashes
72+
expect(hash1.toHex()).not.toBe(hash2.toHex())
73+
})
74+
})
75+
76+
describe('UTF-8 encoding', () => {
77+
it('should handle UTF-8 encoding parameter', () => {
78+
const md = sha1.create()
79+
expect(() => md.update('test string', 'utf8')).not.toThrow()
80+
})
81+
})
82+
83+
describe('ByteStringBuffer input', () => {
84+
it('should hash ByteStringBuffer input', () => {
85+
const buffer = new ByteStringBuffer()
86+
buffer.putBytes('abc')
87+
88+
const md = sha1.create()
89+
const hash = md.update(buffer).digest()
90+
expect(hash).toBeDefined()
91+
expect(hash.toHex().length).toBe(40)
92+
})
93+
})
94+
95+
describe('edge cases', () => {
96+
it('should handle messages that require padding to a new block', () => {
97+
// 64 bytes is exactly one block, so this will require padding in a new block
98+
const md = sha1.create()
99+
const hash = md.update('a'.repeat(64)).digest()
100+
expect(hash).toBeDefined()
101+
expect(hash.toHex().length).toBe(40)
102+
})
103+
104+
it('should handle messages that are exactly one byte less than a block', () => {
105+
// 63 bytes is one byte less than a block
106+
const md = sha1.create()
107+
const hash = md.update('a'.repeat(63)).digest()
108+
expect(hash).toBeDefined()
109+
expect(hash.toHex().length).toBe(40)
110+
})
111+
112+
it('should handle longer messages', () => {
113+
// Test with a longer message (multiple blocks)
114+
const md = sha1.create()
115+
const hash = md.update('a'.repeat(200)).digest()
116+
expect(hash).toBeDefined()
117+
expect(hash.toHex().length).toBe(40)
118+
})
119+
})
120+
121+
describe('state management', () => {
122+
it('should reset state when start() is called', () => {
123+
const md = sha1.create()
124+
md.update('test')
125+
md.start()
126+
expect(md.messageLength).toBe(0)
127+
})
128+
129+
it('should maintain state between updates', () => {
130+
const md1 = sha1.create()
131+
const singleHash = md1.update('abcdef').digest().toHex()
132+
133+
const md2 = sha1.create()
134+
md2.update('abc')
135+
md2.update('def')
136+
const incrementalHash = md2.digest().toHex()
137+
138+
expect(incrementalHash).toBe(singleHash)
139+
})
140+
})
141+
})

0 commit comments

Comments
 (0)