Skip to content

Commit 2b109ad

Browse files
committed
chore: wip
1 parent 77764d3 commit 2b109ad

3 files changed

Lines changed: 56 additions & 77 deletions

File tree

src/algorithms/hash/sha512.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { ByteStringBuffer } from '../../utils'
1313
import { createBuffer, fillString } from '../../utils'
1414

1515
// SHA-512 state interface (each value is represented as two 32-bit integers)
16-
interface SHA512State {
16+
export interface SHA512State {
1717
h0: [number, number]
1818
h1: [number, number]
1919
h2: [number, number]
@@ -39,7 +39,7 @@ interface MessageDigest {
3939
}
4040

4141
// SHA-512 algorithm type
42-
type SHA512Algorithm = 'SHA-512' | 'SHA-384' | 'SHA-512/256' | 'SHA-512/224'
42+
export type SHA512Algorithm = 'SHA-512' | 'SHA-384' | 'SHA-512/256' | 'SHA-512/224'
4343

4444
// Internal state
4545
let _initialized = false

src/pkcs1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
*/
4545

4646
import type { ByteStringBuffer } from './utils'
47-
import { sha1 } from './sha1'
47+
import { sha1 } from './algorithms/hash/sha1'
4848

4949
// Extended Error interface for PKCS1 specific errors
5050
interface PKCS1Error extends Error {

test/unit/ed25519.test.ts

Lines changed: 53 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,75 @@
1-
import { describe, it } from 'bun:test'
2-
import ASSERT from 'node:assert'
1+
import { describe, it, expect } from 'bun:test'
32
import { ed25519 } from '../../src/algorithms/asymmetric/ed25519'
4-
import { SHA256 } from '../../src/sha256'
3+
import { sha256 } from '../../src/algorithms/hash/sha256'
54
import { ByteBuffer, decode64, encode64, hexToBytes } from '../../src/utils'
65

7-
const b64PrivateKey
8-
= 'XohImNooBHFR0OVvjcYpJ3NgPQ1qq73WKhHvch0VQtjE20/RjGhpDRDhAKkH'
9-
+ 'fQjKciEW7zmJamO56uXdT4rr+g=='
6+
const b64PrivateKey = 'XohImNooBHFR0OVvjcYpJ3NgPQ1qq73WKhHvch0VQtjE20/RjGhpDRDhAKkHfQjKciEW7zmJamO56uXdT4rr+g=='
107
const b64PublicKey = 'xNtP0YxoaQ0Q4QCpB30IynIhFu85iWpjuerl3U+K6/o='
11-
const b64Signature
12-
= 'DttvMHiwblQQ+f5uvqebITsJ5YFnDdoU7j4liFaynZeQB65Zs+MkQ2PxA978'
13-
+ 'ALonGdIhCr2chw/sP53pDQVMCw=='
14-
const b64BadSignature
15-
= 'AttvMHiwblQQ+f5uvqebITsJ5YFnDdoU7j4liFaynZeQB65Zs+MkQ2PxA978'
16-
+ 'ALonGdIhCr2chw/sP53pDQVMCw=='
17-
const b64Sha256Signature
18-
= 'sJwlB2ODjzFPe5mlyJHPkryCJDE6r5oVDGGtyPY/eomBKhAogWow/AYuZ9fZ'
19-
+ '/gGg4Jd2ub3SzLnzhkaUPUxQDA=='
8+
const b64Signature = 'DttvMHiwblQQ+f5uvqebITsJ5YFnDdoU7j4liFaynZeQB65Zs+MkQ2PxA978ALonGdIhCr2chw/sP53pDQVMCw=='
9+
const b64BadSignature = 'AttvMHiwblQQ+f5uvqebITsJ5YFnDdoU7j4liFaynZeQB65Zs+MkQ2PxA978ALonGdIhCr2chw/sP53pDQVMCw=='
10+
const b64Sha256Signature = 'sJwlB2ODjzFPe5mlyJHPkryCJDE6r5oVDGGtyPY/eomBKhAogWow/AYuZ9fZ/gGg4Jd2ub3SzLnzhkaUPUxQDA=='
2011

2112
describe('ed25519', () => {
2213
it('should generate a key pair from a seed', () => {
2314
const pwd = 'password'
24-
const md = SHA256.create()
15+
const md = sha256.create()
2516
md.update(pwd, 'utf8')
2617
const seed = md.digest().getBytes()
2718
const kp = ed25519.generateKeyPair({ seed })
2819
const privateKey = eb64(kp.privateKey)
2920
const publicKey = eb64(kp.publicKey)
30-
ASSERT.equal(privateKey, b64PrivateKey)
31-
ASSERT.equal(publicKey, b64PublicKey)
21+
expect(privateKey).toBe(b64PrivateKey)
22+
expect(publicKey).toBe(b64PublicKey)
3223
})
3324

3425
it('should get a public key from a private key', () => {
3526
const privateKey = db64(b64PrivateKey)
3627
const publicKey = ed25519.publicKeyFromPrivateKey({
3728
privateKey,
3829
})
39-
ASSERT.equal(eb64(publicKey), b64PublicKey)
30+
expect(eb64(publicKey)).toBe(b64PublicKey)
4031
})
4132

4233
it('should generate a random key pair', () => {
4334
const kp = ed25519.generateKeyPair()
44-
ASSERT.ok(kp.privateKey)
45-
ASSERT.ok(kp.publicKey)
35+
expect(kp.privateKey).toBeTruthy()
36+
expect(kp.publicKey).toBeTruthy()
4637
})
4738

4839
it('should sign a SHA-256 digest of an UTF-8 message', () => {
4940
const pwd = 'password'
50-
let md = SHA256.create()
41+
let md = sha256.create()
5142
md.update(pwd, 'utf8')
5243
const seed = md.digest().getBytes()
5344
const kp = ed25519.generateKeyPair({ seed })
54-
md = SHA256.create()
45+
md = sha256.create()
5546
md.update('test', 'utf8')
5647
const signature = ed25519.sign({
5748
md,
5849
privateKey: kp.privateKey,
5950
})
60-
ASSERT.equal(eb64(signature), b64Sha256Signature)
51+
expect(eb64(signature)).toBe(b64Sha256Signature)
6152
})
6253

6354
it('should sign a digest given 32 private key bytes', () => {
6455
const pwd = 'password'
65-
let md = SHA256.create()
56+
let md = sha256.create()
6657
md.update(pwd, 'utf8')
6758
const seed = md.digest().getBytes()
6859
const kp = ed25519.generateKeyPair({ seed })
69-
md = SHA256.create()
60+
md = sha256.create()
7061
md.update('test', 'utf8')
7162
const privateKey = kp.privateKey.slice(0, 32)
7263
const signature = ed25519.sign({
7364
md,
7465
privateKey,
7566
})
76-
ASSERT.equal(eb64(signature), b64Sha256Signature)
67+
expect(eb64(signature)).toBe(b64Sha256Signature)
7768
})
7869

7970
it('should sign a UTF-8 message', () => {
8071
const pwd = 'password'
81-
const md = SHA256.create()
72+
const md = sha256.create()
8273
md.update(pwd, 'utf8')
8374
const seed = md.digest().getBytes()
8475
const kp = ed25519.generateKeyPair({ seed })
@@ -87,12 +78,12 @@ describe('ed25519', () => {
8778
encoding: 'utf8',
8879
privateKey: kp.privateKey,
8980
})
90-
ASSERT.equal(eb64(signature), b64Signature)
81+
expect(eb64(signature)).toBe(b64Signature)
9182
})
9283

9384
it('should sign a binary message', () => {
9485
const pwd = 'password'
95-
const md = SHA256.create()
86+
const md = sha256.create()
9687
md.update(pwd, 'utf8')
9788
const seed = md.digest().getBytes()
9889
const kp = ed25519.generateKeyPair({ seed })
@@ -101,25 +92,25 @@ describe('ed25519', () => {
10192
encoding: 'binary',
10293
privateKey: kp.privateKey,
10394
})
104-
ASSERT.equal(eb64(signature), b64Signature)
95+
expect(eb64(signature)).toBe(b64Signature)
10596
})
10697

10798
it('should sign a forge ByteBuffer message', () => {
10899
const pwd = 'password'
109-
const md = SHA256.create()
100+
const md = sha256.create()
110101
md.update(pwd, 'utf8')
111102
const seed = md.digest().getBytes()
112103
const kp = ed25519.generateKeyPair({ seed })
113104
const signature = ed25519.sign({
114105
message: new ByteBuffer('test', 'utf8'),
115106
privateKey: kp.privateKey,
116107
})
117-
ASSERT.equal(eb64(signature), b64Signature)
108+
expect(eb64(signature)).toBe(b64Signature)
118109
})
119110

120111
it('should sign a Uint8Array message', () => {
121112
const pwd = 'password'
122-
const md = SHA256.create()
113+
const md = sha256.create()
123114
md.update(pwd, 'utf8')
124115
const seed = md.digest().getBytes()
125116
const kp = ed25519.generateKeyPair({ seed })
@@ -132,27 +123,27 @@ describe('ed25519', () => {
132123
message,
133124
privateKey: kp.privateKey,
134125
})
135-
ASSERT.equal(eb64(signature), b64Signature)
126+
expect(eb64(signature)).toBe(b64Signature)
136127
})
137128

138129
if (typeof Buffer !== 'undefined') {
139130
it('should sign a node.js Buffer message', () => {
140131
const pwd = 'password'
141-
const md = SHA256.create()
132+
const md = sha256.create()
142133
md.update(pwd, 'utf8')
143134
const seed = md.digest().getBytes()
144135
const kp = ed25519.generateKeyPair({ seed })
145136
const signature = ed25519.sign({
146137
message: Buffer.from('test', 'utf8'),
147138
privateKey: kp.privateKey,
148139
})
149-
ASSERT.equal(eb64(signature), b64Signature)
140+
expect(eb64(signature)).toBe(b64Signature)
150141
})
151142
}
152143

153144
it('should verify a signature', () => {
154145
const pwd = 'password'
155-
const md = SHA256.create()
146+
const md = sha256.create()
156147
md.update(pwd, 'utf8')
157148
const seed = md.digest().getBytes()
158149
const kp = ed25519.generateKeyPair({ seed })
@@ -165,32 +156,32 @@ describe('ed25519', () => {
165156
signature,
166157
publicKey: kp.publicKey,
167158
})
168-
ASSERT.equal(verified, true)
159+
expect(verified).toBe(true)
169160
})
170161

171162
it('should verify a SHA-256 digest signature', () => {
172163
const pwd = 'password'
173-
let md = SHA256.create()
164+
let md = sha256.create()
174165
md.update(pwd, 'utf8')
175166
const seed = md.digest().getBytes()
176167
const kp = ed25519.generateKeyPair({ seed })
177168

178169
const signature = db64(b64Sha256Signature)
179-
md = SHA256.create()
170+
md = sha256.create()
180171
md.update('test', 'utf8')
181172

182173
const verified = ed25519.verify({
183174
md,
184175
signature,
185176
publicKey: kp.publicKey,
186177
})
187-
ASSERT.equal(verified, true)
178+
expect(verified).toBe(true)
188179
})
189180

190181
if (typeof Buffer !== 'undefined') {
191182
it('should verify a node.js Buffer signature', () => {
192183
const pwd = 'password'
193-
const md = SHA256.create()
184+
const md = sha256.create()
194185
md.update(pwd, 'utf8')
195186
const seed = md.digest().getBytes()
196187
const kp = ed25519.generateKeyPair({ seed })
@@ -203,14 +194,14 @@ describe('ed25519', () => {
203194
signature,
204195
publicKey: kp.publicKey,
205196
})
206-
ASSERT.equal(verified, true)
197+
expect(verified).toBe(true)
207198
})
208199
}
209200

210201
it('should generate a random key pair and sign and verify', () => {
211202
const kp = ed25519.generateKeyPair()
212-
ASSERT.ok(kp.privateKey)
213-
ASSERT.ok(kp.publicKey)
203+
expect(kp.privateKey).toBeTruthy()
204+
expect(kp.publicKey).toBeTruthy()
214205

215206
const signature = ed25519.sign({
216207
message: 'test',
@@ -225,12 +216,12 @@ describe('ed25519', () => {
225216
publicKey: kp.publicKey,
226217
})
227218

228-
ASSERT.equal(verified, true)
219+
expect(verified).toBe(true)
229220
})
230221

231222
it('should fail to verify a signature', () => {
232223
const pwd = 'password'
233-
const md = SHA256.create()
224+
const md = sha256.create()
234225
md.update(pwd, 'utf8')
235226
const seed = md.digest().getBytes()
236227
const kp = ed25519.generateKeyPair({ seed })
@@ -243,7 +234,7 @@ describe('ed25519', () => {
243234
signature,
244235
publicKey: kp.publicKey,
245236
})
246-
ASSERT.equal(verified, false)
237+
expect(verified).toBe(false)
247238
})
248239

249240
it('should sign and verify with a base64-decoded key pair', () => {
@@ -253,7 +244,7 @@ describe('ed25519', () => {
253244
encoding: 'utf8',
254245
privateKey,
255246
})
256-
ASSERT.equal(eb64(signature), b64Signature)
247+
expect(eb64(signature)).toBe(b64Signature)
257248

258249
const publicKey = db64(b64PublicKey)
259250
const verified = ed25519.verify({
@@ -262,7 +253,7 @@ describe('ed25519', () => {
262253
signature,
263254
publicKey,
264255
})
265-
ASSERT.equal(verified, true)
256+
expect(verified).toBe(true)
266257
})
267258

268259
it('should pass test vector 1', () => {
@@ -289,23 +280,15 @@ describe('ed25519', () => {
289280
signature,
290281
publicKey,
291282
})
292-
ASSERT.equal(hex(signature), expectedSignature)
293-
ASSERT.equal(verified, true)
283+
expect(hex(signature)).toBe(expectedSignature)
284+
expect(verified).toBe(true)
294285
})
295286

296287
it('should pass test vector 2', () => {
297-
let privateKey = hexToBytes(
298-
'4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb',
299-
)
300-
const publicKey = hexToBytes(
301-
'3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c',
302-
)
288+
let privateKey = hexToBytes('4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb')
289+
const publicKey = hexToBytes('3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c')
303290
privateKey += publicKey
304-
const expectedSignature
305-
= '92a009a9f0d4cab8720e820b5f642540'
306-
+ 'a2b27b5416503f8fb3762223ebdb69da'
307-
+ '085ac1e43e15996e458f3613d0f11d8c'
308-
+ '387b2eaeb4302aeeb00d291612bb0c00'
291+
const expectedSignature = '92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00'
309292

310293
const message = new ByteBuffer()
311294
message.putByte(0x72)
@@ -318,17 +301,13 @@ describe('ed25519', () => {
318301
signature,
319302
publicKey,
320303
})
321-
ASSERT.equal(hex(signature), expectedSignature)
322-
ASSERT.equal(verified, true)
304+
expect(hex(signature)).toBe(expectedSignature)
305+
expect(verified).toBe(true)
323306
})
324307

325308
it('should pass test vector 3', () => {
326-
let privateKey = hexToBytes(
327-
'c5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7',
328-
)
329-
const publicKey = hexToBytes(
330-
'fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025',
331-
)
309+
let privateKey = hexToBytes('c5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7')
310+
const publicKey = hexToBytes('fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025')
332311
privateKey += publicKey
333312
const expectedSignature
334313
= '6291d657deec24024827e69c3abe01a3'
@@ -348,8 +327,8 @@ describe('ed25519', () => {
348327
signature,
349328
publicKey,
350329
})
351-
ASSERT.equal(hex(signature), expectedSignature)
352-
ASSERT.equal(verified, true)
330+
expect(hex(signature)).toBe(expectedSignature)
331+
expect(verified).toBe(true)
353332
})
354333
})
355334

0 commit comments

Comments
 (0)