Skip to content

Commit 0afda05

Browse files
committed
chore: wip
1 parent 2b109ad commit 0afda05

4 files changed

Lines changed: 72 additions & 98 deletions

File tree

src/md.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { md5 } from './algorithms/hash/md5'
22
import { sha1 } from './algorithms/hash/sha1'
33
import { sha256 } from './algorithms/hash/sha256'
4-
import { sha384, sha512 } from './algorithms/hash/sha512'
4+
import { sha384 } from './algorithms/hash/sha384'
5+
import { sha512 } from './algorithms/hash/sha512'
56

67
export interface MessageDigest {
78
sha1: typeof sha1

src/utils/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -545,26 +545,27 @@ export class ByteStringBuffer {
545545
}
546546
}
547547

548+
// Create an alias for ByteStringBuffer for backward compatibility
548549
export const ByteBuffer: typeof ByteStringBuffer = ByteStringBuffer
549550

550-
// Utility functions
551-
552551
/**
553-
* Creates a buffer that stores bytes.
552+
* Creates a new byte buffer.
553+
*
554+
* @param [input] the input to create the byte buffer from.
555+
* @param [encoding] optional encoding ('utf8' or 'binary').
554556
*
555-
* @param [input] a string with encoded bytes to store in the buffer.
556-
* @param [encoding] (default: 'raw', other: 'utf8').
557+
* @return the byte buffer.
557558
*/
558559
export function createBuffer(input?: string, encoding?: string): ByteStringBuffer {
559-
encoding = encoding || 'raw'
560-
561560
if (input !== undefined && encoding === 'utf8') {
562561
input = encodeUtf8(input)
563562
}
564563

565564
return new ByteStringBuffer(input)
566565
}
567566

567+
// Utility functions
568+
568569
/**
569570
* Checks if the given object is an ArrayBuffer.
570571
*

test/unit/sha1.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { describe, it, expect } from 'bun:test'
2+
import { fillString } from '../../src/utils'
3+
import { sha1 } from '../../src/algorithms/hash/sha1'
4+
5+
describe('sha1', () => {
6+
it('should have correct digest length', () => {
7+
const md = sha1.create()
8+
expect(md.digestLength).toBe(20)
9+
})
10+
11+
it('should digest the empty string', () => {
12+
const md = sha1.create()
13+
expect(md.digest().toHex()).toBe('da39a3ee5e6b4b0d3255bfef95601890afd80709')
14+
})
15+
16+
it('should digest "abc"', () => {
17+
const md = sha1.create()
18+
md.update('abc')
19+
expect(md.digest().toHex()).toBe('a9993e364706816aba3e25717850c26c9cd0d89d')
20+
})
21+
22+
it('should digest "The quick brown fox jumps over the lazy dog"', () => {
23+
const md = sha1.create()
24+
md.update('The quick brown fox jumps over the lazy dog')
25+
expect(md.digest().toHex()).toBe('2fd4e1c67a2d28fced849ee1bb76e7391b93eb12')
26+
})
27+
28+
it('should digest "c\'\u00E8"', () => {
29+
const md = sha1.create()
30+
md.update('c\'\u00E8', 'utf8')
31+
expect(md.digest().toHex()).toBe('98c9a3f804daa73b68a5660d032499a447350c0d')
32+
})
33+
34+
it('should digest "THIS IS A MESSAGE"', () => {
35+
const md = sha1.create()
36+
md.start()
37+
md.update('THIS IS ')
38+
md.update('A MESSAGE')
39+
// do twice to check continuing digest
40+
expect(md.digest().toHex()).toBe('5f24f4d6499fd2d44df6c6e94be8b14a796c071d')
41+
expect(md.digest().toHex()).toBe('5f24f4d6499fd2d44df6c6e94be8b14a796c071d')
42+
})
43+
44+
it('should digest a long message', () => {
45+
// Note: might be too slow on old browsers
46+
const md = sha1.create()
47+
md.update(fillString('a', 1000000))
48+
expect(md.digest().toHex()).toBe('34aa973cd4c4daa4f61eeb2bdbad27316534016f')
49+
})
50+
51+
it('should digest multiple long messages', () => {
52+
// Note: might be too slow on old browsers
53+
// done multiple times to check hot loop optimizations
54+
for (let loop = 0; loop < 3; ++loop) {
55+
const md = sha1.create()
56+
for (let i = 0; i < 10000; ++i) {
57+
md.update('abc')
58+
}
59+
expect(md.digest().toHex()).toBe('a838edb5dec47b84b4bfb0a528ea958a5d9d2350')
60+
}
61+
})
62+
})

test/unit/sha1.ts

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

0 commit comments

Comments
 (0)