crypto.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var crypto_exports = {};
  19. __export(crypto_exports, {
  20. createHash: () => createHash,
  21. md5: () => md5,
  22. sha1: () => sha1,
  23. sha256: () => sha256
  24. });
  25. module.exports = __toCommonJS(crypto_exports);
  26. const sha256 = async (data) => {
  27. const algorithm = { name: "SHA-256", alias: "sha256" };
  28. const hash = await createHash(data, algorithm);
  29. return hash;
  30. };
  31. const sha1 = async (data) => {
  32. const algorithm = { name: "SHA-1", alias: "sha1" };
  33. const hash = await createHash(data, algorithm);
  34. return hash;
  35. };
  36. const md5 = async (data) => {
  37. const algorithm = { name: "MD5", alias: "md5" };
  38. const hash = await createHash(data, algorithm);
  39. return hash;
  40. };
  41. const createHash = async (data, algorithm) => {
  42. let sourceBuffer;
  43. if (ArrayBuffer.isView(data) || data instanceof ArrayBuffer) {
  44. sourceBuffer = data;
  45. } else {
  46. if (typeof data === "object") {
  47. data = JSON.stringify(data);
  48. }
  49. sourceBuffer = new TextEncoder().encode(String(data));
  50. }
  51. if (crypto && crypto.subtle) {
  52. const buffer = await crypto.subtle.digest(
  53. {
  54. name: algorithm.name
  55. },
  56. sourceBuffer
  57. );
  58. const hash = Array.prototype.map.call(new Uint8Array(buffer), (x) => ("00" + x.toString(16)).slice(-2)).join("");
  59. return hash;
  60. }
  61. return null;
  62. };
  63. // Annotate the CommonJS export names for ESM import in node:
  64. 0 && (module.exports = {
  65. createHash,
  66. md5,
  67. sha1,
  68. sha256
  69. });