Skip to content

Commit

Permalink
Merge pull request #43 from FortKnoxster/diefi
Browse files Browse the repository at this point in the history
Added new hashString function
  • Loading branch information
mickeyjoes authored Sep 26, 2022
2 parents b5cae23 + 36dd2ce commit 2b83bb9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export {

export * from './kryptos/utils.js'

export { hashAnything, fingerprint } from './kryptos/digest.js'
export { hashString, hashAnything, fingerprint } from './kryptos/digest.js'

export {
initProtocol,
Expand Down
18 changes: 13 additions & 5 deletions src/kryptos/digest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ export function fingerprint(key) {
return kryptos.subtle.digest(SHA_256.name, objectToArrayBuffer(key))
}

export async function hashString(string, algorithm = SHA_256.name) {
try {
const hash = await kryptos.subtle.digest(
algorithm,
stringToArrayBuffer(string),
)
return arrayBufferToHex(hash)
} catch (err) {
return Promise.reject()
}
}

export async function hashAnything(algorithm = SHA_256.name, ...objects) {
const strBuffer = objects.reduce(
(acc, object) => acc + JSON.stringify(object),
'',
)
const hash = await kryptos.subtle.digest(
algorithm,
stringToArrayBuffer(strBuffer),
)
return arrayBufferToHex(hash)
return hashString(strBuffer, algorithm)
}

0 comments on commit 2b83bb9

Please sign in to comment.