UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

45 lines (42 loc) • 1.65 kB
import { bytesToBase64, bytesToHex, utf8Encode } from "./bytes.native.js"; import { bufferSourceRange } from "./digest.native.js"; var DIGEST_ALGORITHMS = [ "MD5", "SHA-1", "SHA-256", "SHA-384", "SHA-512" ]; function assertDigestAlgorithm(algorithm, verb) { if (!DIGEST_ALGORITHMS.includes(algorithm)) { throw new Error(`${verb}: unknown algorithm ${JSON.stringify(algorithm)}. Expected one of: ${DIGEST_ALGORITHMS.join(", ")}`); } } function createDigestApi(source) { async function digest(algorithm, data) { assertDigestAlgorithm(algorithm, "Crypto.digest"); var { buffer, offset, length } = bufferSourceRange(data, "Crypto.digest"); return source(algorithm, buffer, offset, length); } async function digestString(algorithm, data) { var options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {}; var _ref; assertDigestAlgorithm(algorithm, "Crypto.digestString"); if (typeof data !== "string") { throw new TypeError("Crypto.digestString: data must be a string"); } var encoding = (_ref = options === null || options === void 0 ? void 0 : options.encoding) !== null && _ref !== void 0 ? _ref : "hex"; if (encoding !== "hex" && encoding !== "base64") { throw new Error(`Crypto.digestString: encoding must be 'hex' or 'base64'`); } var bytes = utf8Encode(data); var hashed = new Uint8Array(await source(algorithm, bytes.buffer, 0, bytes.length)); return encoding === "base64" ? bytesToBase64(hashed) : bytesToHex(hashed); } return { digest, digestString }; } export { DIGEST_ALGORITHMS, assertDigestAlgorithm, createDigestApi }; //# sourceMappingURL=digest-api.native.js.map