Skip to content

Commit

Permalink
feat(x): 新增 sha1
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Dec 24, 2020
1 parent ee055b7 commit d69393a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/x/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export * from './createXml'
export * from './cuid'
export * from './parseXml'
export * from './RedisCookieJar'
export * from './sha1'
export * from './uuid'
// @endindex
10 changes: 10 additions & 0 deletions src/x/sha1.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { sha1 } from './sha1'

describe('sha1', () => {
test('表现正常', () => {
expect(sha1('foo')).toBe('0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33')
expect(sha1(Buffer.from('foo'))).toBe(
'0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33',
)
})
})
10 changes: 10 additions & 0 deletions src/x/sha1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import crypto, { BinaryLike } from 'crypto'

/**
* 获取给定值的 sha1 哈希。
*
* @param value 给定值
*/
export function sha1(value: BinaryLike): string {
return crypto.createHash('sha1').update(value).digest('hex')
}

0 comments on commit d69393a

Please sign in to comment.