-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a react-native section to package.json, split off from browser file.
- Loading branch information
1 parent
00f496a
commit 13286c6
Showing
3 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. | ||
*/ | ||
'use strict'; | ||
|
||
const crypto = require('isomorphic-webcrypto'); | ||
require('fast-text-encoding'); | ||
|
||
module.exports = class MessageDigest { | ||
/** | ||
* Creates a new MessageDigest. | ||
* | ||
* @param algorithm the algorithm to use. | ||
*/ | ||
constructor(algorithm) { | ||
// check if crypto.subtle is available | ||
// check is here rather than top-level to only fail if class is used | ||
if(!(crypto && crypto.subtle)) { | ||
throw new Error('crypto.subtle not found.'); | ||
} | ||
if(algorithm === 'sha256') { | ||
this.algorithm = {name: 'SHA-256'}; | ||
} else if(algorithm === 'sha1') { | ||
this.algorithm = {name: 'SHA-1'}; | ||
} else { | ||
throw new Error(`Unsupported algorithm "${algorithm}".`); | ||
} | ||
this._content = ''; | ||
} | ||
|
||
update(msg) { | ||
this._content += msg; | ||
} | ||
|
||
async digest() { | ||
const data = new TextEncoder().encode(this._content); | ||
const buffer = new Uint8Array( | ||
await crypto.subtle.digest(this.algorithm, data)); | ||
// return digest in hex | ||
let hex = ''; | ||
for(let i = 0; i < buffer.length; ++i) { | ||
hex += buffer[i].toString(16).padStart(2, '0'); | ||
} | ||
return hex; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters