one
Version:
One is a React framework for web, iOS, and Android apps.
66 lines (65 loc) • 2.42 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var digest_api_exports = {};
__export(digest_api_exports, {
DIGEST_ALGORITHMS: () => DIGEST_ALGORITHMS,
assertDigestAlgorithm: () => assertDigestAlgorithm,
createDigestApi: () => createDigestApi
});
module.exports = __toCommonJS(digest_api_exports);
var import_bytes = require("./bytes.cjs");
var import_digest = require("./digest.cjs");
const 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");
const { buffer, offset, length } = (0, import_digest.bufferSourceRange)(data, "Crypto.digest");
return source(algorithm, buffer, offset, length);
}
async function digestString(algorithm, data, options = {}) {
assertDigestAlgorithm(algorithm, "Crypto.digestString");
if (typeof data !== "string") {
throw new TypeError("Crypto.digestString: data must be a string");
}
const encoding = options?.encoding ?? "hex";
if (encoding !== "hex" && encoding !== "base64") {
throw new Error(`Crypto.digestString: encoding must be 'hex' or 'base64'`);
}
const bytes = (0, import_bytes.utf8Encode)(data);
const hashed = new Uint8Array(await source(algorithm, bytes.buffer, 0, bytes.length));
return encoding === "base64" ? (0, import_bytes.bytesToBase64)(hashed) : (0, import_bytes.bytesToHex)(hashed);
}
return {
digest,
digestString
};
}