UNPKG

@onesy/utils

Version:
21 lines (19 loc) 766 B
import SHA256 from 'crypto-js/sha256'; import to from './to'; import isEnvironment from './isEnvironment'; import fileToValue from './fileToValue'; const optionsDefault = { withPrefix: true }; const hashFile = async function (value_) { let options_ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; const options = { ...optionsDefault, ...options_ }; let value = value_; value = isEnvironment('browser') ? await fileToValue(value, 'array-buffer') : to(Buffer.from(value), 'arraybuffer'); value = isEnvironment('browser') ? String.fromCharCode.apply(null, new Uint8Array(value)) : to(value, 'string'); value = SHA256(value).toString(); return options.withPrefix ? "0x".concat(value) : value; }; export default hashFile;