-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* Copyright (c) 2018-2019 (original work) Open Assessment Technologies SA | ||
* Copyright (c) 2018-2020 (original work) Open Assessment Technologies SA | ||
* | ||
*/ | ||
|
||
|
@@ -24,13 +24,12 @@ | |
* | ||
* @author Bertrand Chevrier <[email protected]> | ||
*/ | ||
import _ from 'lodash'; | ||
import 'webcrypto-shim'; | ||
import { TextEncoder } from 'fastestsmallesttextencoderdecoder'; | ||
|
||
//get the native implementation of the CryptoSubtle | ||
var subtle = window.crypto.subtle || window.crypto.webkitSubtle; | ||
var supportedAlgorithms = [ | ||
const subtle = window.crypto.subtle || window.crypto.webkitSubtle; | ||
const supportedAlgorithms = [ | ||
'SHA-1', //considered as not safe anymore | ||
'SHA-256', | ||
'SHA-384', | ||
|
@@ -39,37 +38,44 @@ var supportedAlgorithms = [ | |
|
||
/** | ||
* Encode a buffer to an hexadecimal string | ||
* @param {Number[]|ArrayBuffer} buffer | ||
* @returns {String} the hex representation of the buffer | ||
* @param {number[]|ArrayBuffer} buffer | ||
* @returns {string} the hex representation of the buffer | ||
*/ | ||
var bufferToHexString = function bufferToHexString(buffer) { | ||
return [].map | ||
.call(new Uint8Array(buffer), function(val) { | ||
return ('00' + val.toString(16)).slice(-2); | ||
}) | ||
.join(''); | ||
}; | ||
function bufferToHexString(buffer) { | ||
return [...new Uint8Array(buffer)].map(val => `00${val.toString(16)}`.slice(-2)).join(''); | ||
} | ||
|
||
/** | ||
* Create a hash/checksum from a given string | ||
* @param {String} utf8String - the string to hash | ||
* @param {String} [selectedAlgorithm = 'SHA-256'] - how to hash | ||
* Create a hash/checksum from a given string, blob or buffer | ||
* @param {string|Blob|ArrayBuffer|Uint8Array} data - the data to hash | ||
* @param {string} [selectedAlgorithm] - hashing algorithm | ||
* @returns {Promise<String>} resolves with the hash of the string | ||
* @throws {TypeError} if the algorithm is not available or the input string is missing | ||
*/ | ||
export default function digest(utf8String, selectedAlgorithm) { | ||
var algorithm; | ||
if (!_.isString(selectedAlgorithm)) { | ||
selectedAlgorithm = 'SHA-256'; | ||
} | ||
algorithm = selectedAlgorithm.toUpperCase(); | ||
if (!_.contains(supportedAlgorithms, algorithm)) { | ||
throw new TypeError('Unsupported digest algorithm : ' + algorithm); | ||
export default function digest(data, selectedAlgorithm = 'SHA-256') { | ||
let algorithm = selectedAlgorithm.toUpperCase(); | ||
if (!supportedAlgorithms.includes(algorithm)) { | ||
throw new TypeError(`Unsupported digest algorithm : ${algorithm}`); | ||
} | ||
if (!_.isString(utf8String)) { | ||
throw new TypeError('Please encode a string, not a ' + typeof utf8String); | ||
|
||
let dataPromise; | ||
if (data instanceof Uint8Array) { | ||
dataPromise = Promise.resolve(data); | ||
} else if (data instanceof ArrayBuffer) { | ||
dataPromise = Promise.resolve(new Uint8Array([data])); | ||
} else if (data instanceof Blob) { | ||
dataPromise = new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.addEventListener('loadend', () => resolve(reader.result)); | ||
reader.addEventListener('abort', reject); | ||
reader.addEventListener('error', reject); | ||
reader.readAsArrayBuffer(data); | ||
}); | ||
} else if (typeof data === 'string') { | ||
dataPromise = Promise.resolve(new TextEncoder('utf-8').encode(data)); | ||
} else { | ||
throw new TypeError(`Unsupported data type to digest with ${algorithm}`); | ||
} | ||
return subtle.digest(algorithm, new TextEncoder('utf-8').encode(utf8String)).then(function(buffer) { | ||
return bufferToHexString(buffer); | ||
}); | ||
|
||
return dataPromise.then(rawData => subtle.digest(algorithm, rawData)).then(buffer => bufferToHexString(buffer)); | ||
} |
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