Skip to content

feat: add sha1 support #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -148,6 +148,11 @@
"types": "./dist/types/src/hashes/interface.d.ts",
"import": "./src/hashes/interface.js"
},
"./hashes/sha1": {
"types": "./dist/types/src/hashes/sha1.d.ts",
"browser": "./src/hashes/sha1-browser.js",
"import": "./src/hashes/sha1.js"
},
"./hashes/sha2": {
"types": "./dist/types/src/hashes/sha2.d.ts",
"browser": "./src/hashes/sha2-browser.js",
@@ -171,6 +176,8 @@
}
},
"browser": {
"./hashes/sha1": "./src/hashes/sha1-browser.js",
"./src/hashes/sha1.js": "./src/hashes/sha1-browser.js",
"./hashes/sha2": "./src/hashes/sha2-browser.js",
"./src/hashes/sha2.js": "./src/hashes/sha2-browser.js"
},
@@ -286,7 +293,8 @@
"@types/node": "^20.3.1",
"aegir": "^40.0.11",
"buffer": "^6.0.3",
"cids": "^1.1.9"
"cids": "^1.1.9",
"crypto-hash": "^2.0.1"
},
"aegir": {
"test": {
18 changes: 18 additions & 0 deletions src/hashes/sha1-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* global crypto */

import { from } from './hasher.js'

/**
* @param {AlgorithmIdentifier} name
*/
const sha = name =>
/**
* @param {Uint8Array} data
*/
async data => new Uint8Array(await crypto.subtle.digest(name, data))

export const sha1 = from({
name: 'sha-1',
code: 0x11,
encode: sha('SHA-1')
})
11 changes: 11 additions & 0 deletions src/hashes/sha1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-check

import crypto from 'crypto'
import { coerce } from '../bytes.js'
import { from } from './hasher.js'

export const sha1 = from({
name: 'sha-1',
code: 0x11,
encode: (input) => coerce(crypto.createHash('sha1').update(input).digest())
})
28 changes: 28 additions & 0 deletions test/test-multihash.spec.js
Original file line number Diff line number Diff line change
@@ -3,9 +3,11 @@
import { hash as slSha256 } from '@stablelib/sha256'
import { hash as slSha512 } from '@stablelib/sha512'
import { assert } from 'aegir/chai'
import { sha1 as chSha1 } from 'crypto-hash'
import { fromHex, fromString } from '../src/bytes.js'
import { decode as decodeDigest, create as createDigest } from '../src/hashes/digest.js'
import { identity } from '../src/hashes/identity.js'
import { sha1 } from '../src/hashes/sha1.js'
import { sha256, sha512 } from '../src/hashes/sha2.js'
import invalid from './fixtures/invalid-multihash.js'
import valid from './fixtures/valid-multihash.js'
@@ -40,6 +42,32 @@ describe('multihash', () => {
}
})

it('hash sha1', async () => {
const hash = await sha1.digest(fromString('test'))
assert.deepStrictEqual(hash.code, sha1.code)
assert.deepStrictEqual(hash.digest, fromHex(await chSha1(fromString('test'))))

const hash2 = decodeDigest(hash.bytes)
assert.deepStrictEqual(hash2.code, sha1.code)
assert.deepStrictEqual(hash2.bytes, hash.bytes)
})

if (typeof navigator === 'undefined') {
it('sync sha1', async () => {
const hash = sha1.digest(fromString('test'))
if (hash instanceof Promise) {
assert.fail('expected sync result')
} else {
assert.deepStrictEqual(hash.code, sha1.code)
assert.deepStrictEqual(hash.digest, fromHex(await chSha1(fromString('test'))))

const hash2 = decodeDigest(hash.bytes)
assert.deepStrictEqual(hash2.code, sha1.code)
assert.deepStrictEqual(hash2.bytes, hash.bytes)
}
})
}

it('hash sha2-256', async () => {
const hash = await sha256.digest(fromString('test'))
assert.deepStrictEqual(hash.code, sha256.code)