• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tinyhttp / malibu / 6760091955

05 Nov 2023 08:07AM UTC coverage: 99.712% (+2.0%) from 97.759%
6760091955

push

github

web-flow
Merge pull request #5 from tinyhttp/chore/maintenance-october-2023

chore: dependency maintenance october 2023

62 of 63 branches covered (0.0%)

Branch coverage included in aggregate %.

284 of 284 relevant lines covered (100.0%)

23.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/utils.ts
1
import { randomBytes, createHash, createHmac, timingSafeEqual } from 'crypto'
1✔
2

1✔
3
/**
1✔
4
 * Generate a secure type safe UID with specified length.
1✔
5
 * Rewrite of https://github.com/crypto-utils/uid-safe
1✔
6
 * @param {Number} length UID length
1✔
7
 * @returns {String} Type safe UID
1✔
8
 */
1✔
9
export function typeSafeUID(length: number): string {
1✔
10
  return randomBytes(length).toString('base64').replace(/=+$/, '').replace(/\+/g, '-').replace(/\//g, '_')
32✔
11
}
32✔
12

1✔
13
/**
1✔
14
 * Generate a (not secure) quick salt with specified length.
1✔
15
 * Rewrite of https://github.com/crypto-utils/rndm
1✔
16
 * @param {Number} length Salt length
1✔
17
 * @returns {String} Salt
1✔
18
 */
1✔
19
export function randomBase62(length: number): string {
1✔
20
  const availableCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
32✔
21
  const byteLength = Buffer.byteLength(availableCharacters)
32✔
22
  let salt = ''
32✔
23
  for (let i = 0; i < length; i++) {
32✔
24
    salt += availableCharacters[Math.floor(byteLength * Math.random())]
262✔
25
  }
262✔
26

32✔
27
  return salt
32✔
28
}
32✔
29

1✔
30
/**
1✔
31
 * Create a SHA1 hash from specified string
1✔
32
 * Rewrite of a function from https://github.com/pillarjs/csrf
1✔
33
 * @param {String} str Dirty string
1✔
34
 * @returns {String} Hashed string
1✔
35
 */
1✔
36
export function hash(str: string): string {
1✔
37
  return createHash('sha256')
56✔
38
    .update(str, 'ascii')
56✔
39
    .digest('base64')
56✔
40
    .replace(/=+$/, '')
56✔
41
    .replace(/\+/g, '-')
56✔
42
    .replace(/\//g, '_')
56✔
43
}
56✔
44

1✔
45
/**
1✔
46
 * Compare two keys, to check if they have the same creation duration.
1✔
47
 * Rewrite of https://github.com/suryagh/tsscmp
1✔
48
 * @param {String} a Key #1
1✔
49
 * @param {String} b Key #2
1✔
50
 * @returns {Boolean} Whether both keys matches or not
1✔
51
 */
1✔
52
export function timeSafeCompare(a: string, b: string): boolean {
1✔
53
  const key = randomBytes(32)
27✔
54
  const aHMAC = createHmac('sha256', key).update(String(a)).digest()
27✔
55
  const bHMAC = createHmac('sha256', key).update(String(b)).digest()
27✔
56

27✔
57
  return timingSafeEqual(aHMAC, bHMAC)
27✔
58
}
27✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc