crypto.test.ts
This file
Path
src/utils/crypto.test.ts
43 lines · 1.5 KiB · TypeScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–43why, one step back1import { createHash } from 'crypto'2import { md5, sha1, sha256 } from './crypto'34describe('crypto', () => {5 it('sha256', async () => {6 expect(await sha256('hono')).toBe(7 '8b3dc17add91b7e8f0b5109a389927d66001139cd9b03fa7b95f83126e1b2b23'8 )9 expect(await sha256('炎')).toBe(10 '1fddc5a562ee1fbeb4fc6def7d4be4911fcdae4273b02ae3a507b170ba0ea169'11 )12 expect(await sha256('abcdedf')).not.toBe('abcdef')13 })1415 it('sha1', async () => {16 expect(await sha1('hono')).toBe('28c7e86f5732391917876b45c06c626c04d77f39')17 expect(await sha1('炎')).toBe('d56e09ae2421b2b8a0b5ee5fdceaed663c8c9472')18 expect(await sha1('abcdedf')).not.toBe('abcdef')19 })2021 // MD5 is not part of the WebCrypto standard.22 // Node.js' Web Crypto API does not support it (But Cloudflare Workers supports it).23 // We should skip this test in a Node.js environment.24 it.skip('md5', async () => {25 expect(await md5('hono')).toBe('cf22a160789a91dd5f737cd3b2640cc2')26 expect(await md5('炎')).toBe('f620d89a5a782c22b4420acb39121be3')27 expect(await md5('abcdedf')).not.toBe('abcdef')28 })2930 it('Should not be the same values - compare difference objects', async () => {31 expect(await sha256({ foo: 'bar' })).not.toEqual(32 await sha256({33 bar: 'foo',34 })35 )36 })3738 it('Should create hash for Buffer', async () => {39 const hash = createHash('sha256').update(new Uint8Array(1)).digest('hex')40 expect(await sha256(new Uint8Array(1))).toBe(hash)41 expect(await sha256(new Uint8Array(1))).not.toEqual(await sha256(new Uint8Array(2)))42 })43})