@ocap/mcrypto 1.27.15 → 1.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/esm/crypter/aes-legacy.d.ts +8 -6
  2. package/esm/crypter/aes-legacy.js +27 -24
  3. package/esm/crypter/aes.d.ts +9 -7
  4. package/esm/crypter/aes.js +27 -25
  5. package/esm/crypter/rsa-browserify.d.ts +7 -6
  6. package/esm/crypter/rsa-browserify.js +34 -31
  7. package/esm/crypter/rsa.d.ts +11 -9
  8. package/esm/crypter/rsa.js +31 -28
  9. package/esm/encode.d.ts +12 -8
  10. package/esm/encode.js +13 -18
  11. package/esm/hasher/keccak.d.ts +8 -5
  12. package/esm/hasher/keccak.js +37 -36
  13. package/esm/hasher/sha2.d.ts +8 -5
  14. package/esm/hasher/sha2.js +42 -40
  15. package/esm/hasher/sha3.d.ts +8 -5
  16. package/esm/hasher/sha3.js +37 -36
  17. package/esm/index.d.ts +148 -143
  18. package/esm/index.js +172 -209
  19. package/esm/protocols/crypter.d.ts +3 -1
  20. package/esm/protocols/crypter.js +7 -4
  21. package/esm/protocols/hasher.d.ts +3 -1
  22. package/esm/protocols/hasher.js +7 -4
  23. package/esm/protocols/signer.d.ts +3 -1
  24. package/esm/protocols/signer.js +7 -4
  25. package/esm/signer/ed25519.d.ts +49 -46
  26. package/esm/signer/ed25519.js +80 -80
  27. package/esm/signer/ethereum.d.ts +11 -8
  28. package/esm/signer/ethereum.js +34 -34
  29. package/esm/signer/passkey.d.ts +21 -18
  30. package/esm/signer/passkey.js +49 -58
  31. package/esm/signer/secp256k1.d.ts +35 -32
  32. package/esm/signer/secp256k1.js +80 -93
  33. package/lib/_virtual/rolldown_runtime.js +29 -0
  34. package/lib/crypter/aes-legacy.d.ts +8 -6
  35. package/lib/crypter/aes-legacy.js +35 -52
  36. package/lib/crypter/aes.d.ts +9 -7
  37. package/lib/crypter/aes.js +29 -29
  38. package/lib/crypter/rsa-browserify.d.ts +7 -6
  39. package/lib/crypter/rsa-browserify.js +36 -33
  40. package/lib/crypter/rsa.d.ts +11 -9
  41. package/lib/crypter/rsa.js +33 -32
  42. package/lib/encode.d.ts +12 -8
  43. package/lib/encode.js +14 -21
  44. package/lib/hasher/keccak.d.ts +8 -5
  45. package/lib/hasher/keccak.js +40 -41
  46. package/lib/hasher/sha2.d.ts +8 -5
  47. package/lib/hasher/sha2.js +43 -42
  48. package/lib/hasher/sha3.d.ts +8 -5
  49. package/lib/hasher/sha3.js +40 -41
  50. package/lib/index.d.ts +148 -143
  51. package/lib/index.js +177 -216
  52. package/lib/protocols/crypter.d.ts +3 -1
  53. package/lib/protocols/crypter.js +10 -9
  54. package/lib/protocols/hasher.d.ts +3 -1
  55. package/lib/protocols/hasher.js +10 -9
  56. package/lib/protocols/signer.d.ts +3 -1
  57. package/lib/protocols/signer.js +10 -9
  58. package/lib/signer/ed25519.d.ts +49 -46
  59. package/lib/signer/ed25519.js +85 -86
  60. package/lib/signer/ethereum.d.ts +11 -8
  61. package/lib/signer/ethereum.js +38 -39
  62. package/lib/signer/passkey.d.ts +21 -18
  63. package/lib/signer/passkey.js +51 -63
  64. package/lib/signer/secp256k1.d.ts +35 -32
  65. package/lib/signer/secp256k1.js +85 -99
  66. package/package.json +25 -10
@@ -1,8 +1,10 @@
1
- import Crypter from '../protocols/crypter';
2
- declare class AesCrypter extends Crypter {
3
- encrypt(message: string | object, secret: string): string;
4
- decrypt(cipher: string, secret: string, outputEncoding?: string): string;
1
+ import _default$1 from "../protocols/crypter.js";
2
+
3
+ //#region src/crypter/aes-legacy.d.ts
4
+ declare class AesCrypter extends _default$1 {
5
+ encrypt(message: string | object, secret: string): string;
6
+ decrypt(cipher: string, secret: string, outputEncoding?: string): string;
5
7
  }
6
8
  declare const _default: AesCrypter;
7
- export default _default;
8
- export { AesCrypter };
9
+ //#endregion
10
+ export { AesCrypter, _default as default };
@@ -1,26 +1,29 @@
1
- import * as AES from 'crypto-js/aes';
2
- import encLatin1 from 'crypto-js/enc-latin1';
3
- import encUtf8 from 'crypto-js/enc-utf8';
4
- import encUtf16 from 'crypto-js/enc-utf16';
5
- import encBase64 from 'crypto-js/enc-base64';
6
- import encHex from 'crypto-js/enc-hex';
7
- import Crypter from '../protocols/crypter';
1
+ import crypter_default from "../protocols/crypter.js";
2
+ import * as AES from "crypto-js/aes.js";
3
+ import encLatin1 from "crypto-js/enc-latin1.js";
4
+ import encUtf8 from "crypto-js/enc-utf8.js";
5
+ import encUtf16 from "crypto-js/enc-utf16.js";
6
+ import encBase64 from "crypto-js/enc-base64.js";
7
+ import encHex from "crypto-js/enc-hex.js";
8
+
9
+ //#region src/crypter/aes-legacy.ts
8
10
  const encoders = {
9
- latin1: encLatin1,
10
- utf8: encUtf8,
11
- hex: encHex,
12
- utf16: encUtf16,
13
- base64: encBase64,
11
+ latin1: encLatin1,
12
+ utf8: encUtf8,
13
+ hex: encHex,
14
+ utf16: encUtf16,
15
+ base64: encBase64
14
16
  };
15
- // AES-CBC-256
16
- class AesCrypter extends Crypter {
17
- encrypt(message, secret) {
18
- const text = typeof message === 'string' ? message : JSON.stringify(message);
19
- return AES.encrypt(text, secret).toString();
20
- }
21
- decrypt(cipher, secret, outputEncoding = 'utf8') {
22
- return AES.decrypt(cipher, secret).toString(encoders[outputEncoding]);
23
- }
24
- }
25
- export default new AesCrypter();
26
- export { AesCrypter };
17
+ var AesCrypter = class extends crypter_default {
18
+ encrypt(message, secret) {
19
+ const text = typeof message === "string" ? message : JSON.stringify(message);
20
+ return AES.encrypt(text, secret).toString();
21
+ }
22
+ decrypt(cipher, secret, outputEncoding = "utf8") {
23
+ return AES.decrypt(cipher, secret).toString(encoders[outputEncoding]);
24
+ }
25
+ };
26
+ var aes_legacy_default = new AesCrypter();
27
+
28
+ //#endregion
29
+ export { AesCrypter, aes_legacy_default as default };
@@ -1,9 +1,11 @@
1
- import { BytesType, EncodingType } from '@ocap/util';
2
- import Crypter from '../protocols/crypter';
3
- declare class AesCrypter extends Crypter {
4
- encrypt(message: BytesType, secret: BytesType, encoding?: EncodingType): BytesType;
5
- decrypt(message: BytesType, secret: BytesType, encoding?: EncodingType): BytesType;
1
+ import _default$1 from "../protocols/crypter.js";
2
+ import { BytesType, EncodingType } from "@ocap/util";
3
+
4
+ //#region src/crypter/aes.d.ts
5
+ declare class AesCrypter extends _default$1 {
6
+ encrypt(message: BytesType, secret: BytesType, encoding?: EncodingType): BytesType;
7
+ decrypt(message: BytesType, secret: BytesType, encoding?: EncodingType): BytesType;
6
8
  }
7
9
  declare const _default: AesCrypter;
8
- export default _default;
9
- export { AesCrypter };
10
+ //#endregion
11
+ export { AesCrypter, _default as default };
@@ -1,25 +1,27 @@
1
- // For browsers, may need: https://www.npmjs.com/package/crypto-browserify
2
- import crypto from 'crypto';
3
- import { toBuffer } from '@ocap/util';
4
- import Crypter from '../protocols/crypter';
5
- import SHA3 from '../hasher/sha3';
6
- import { encode } from '../encode';
7
- // AES-ECB-256
8
- class AesCrypter extends Crypter {
9
- encrypt(message, secret, encoding = 'hex') {
10
- const key = SHA3.hash256(secret, 1, 'buffer');
11
- const cipher = crypto.createCipheriv('aes-256-ecb', key, '');
12
- cipher.setAutoPadding(true);
13
- const output = cipher.update(toBuffer(message));
14
- return encode(Buffer.concat([output, cipher.final()]), encoding);
15
- }
16
- decrypt(message, secret, encoding = 'hex') {
17
- const key = SHA3.hash256(secret, 1, 'buffer');
18
- const decipher = crypto.createDecipheriv('aes-256-ecb', key, '');
19
- decipher.setAutoPadding(true);
20
- const output = decipher.update(toBuffer(message));
21
- return encode(Buffer.concat([output, decipher.final()]), encoding);
22
- }
23
- }
24
- export default new AesCrypter();
25
- export { AesCrypter };
1
+ import { encode } from "../encode.js";
2
+ import sha3_default from "../hasher/sha3.js";
3
+ import crypter_default from "../protocols/crypter.js";
4
+ import { toBuffer } from "@ocap/util";
5
+ import crypto from "crypto";
6
+
7
+ //#region src/crypter/aes.ts
8
+ var AesCrypter = class extends crypter_default {
9
+ encrypt(message, secret, encoding = "hex") {
10
+ const key = sha3_default.hash256(secret, 1, "buffer");
11
+ const cipher = crypto.createCipheriv("aes-256-ecb", key, "");
12
+ cipher.setAutoPadding(true);
13
+ const output = cipher.update(toBuffer(message));
14
+ return encode(Buffer.concat([output, cipher.final()]), encoding);
15
+ }
16
+ decrypt(message, secret, encoding = "hex") {
17
+ const key = sha3_default.hash256(secret, 1, "buffer");
18
+ const decipher = crypto.createDecipheriv("aes-256-ecb", key, "");
19
+ decipher.setAutoPadding(true);
20
+ const output = decipher.update(toBuffer(message));
21
+ return encode(Buffer.concat([output, decipher.final()]), encoding);
22
+ }
23
+ };
24
+ var aes_default = new AesCrypter();
25
+
26
+ //#endregion
27
+ export { AesCrypter, aes_default as default };
@@ -1,9 +1,10 @@
1
+ //#region src/crypter/rsa-browserify.d.ts
1
2
  declare class RSABrowserCrypter {
2
- genKeyPair(length?: number): Promise<CryptoKeyPair>;
3
- formatPublicKey(key: CryptoKey): Promise<string>;
4
- encrypt(message: string, key: CryptoKey): Promise<string>;
5
- decrypt(message: string, key: CryptoKey): Promise<string>;
3
+ genKeyPair(length?: number): Promise<CryptoKeyPair>;
4
+ formatPublicKey(key: CryptoKey): Promise<string>;
5
+ encrypt(message: string, key: CryptoKey): Promise<string>;
6
+ decrypt(message: string, key: CryptoKey): Promise<string>;
6
7
  }
7
8
  declare const _default: RSABrowserCrypter;
8
- export default _default;
9
- export { RSABrowserCrypter };
9
+ //#endregion
10
+ export { RSABrowserCrypter, _default as default };
@@ -1,33 +1,36 @@
1
- // https://stackoverflow.com/questions/70056340/how-can-i-generate-an-rsa-pair-that-works-both-in-node-js-and-browser
2
- // https://stackoverflow.com/questions/62948516/using-native-javascript-subtlecrypto-to-encrypt-using-rsa
3
- import { fromBase58, toBase58 } from '@ocap/util';
1
+ import { fromBase58, toBase58 } from "@ocap/util";
2
+
3
+ //#region src/crypter/rsa-browserify.ts
4
4
  const crypto = window.crypto.subtle;
5
- // @ts-ignore
6
5
  const ab2str = (buffer) => String.fromCharCode.apply(null, new Uint8Array(buffer));
7
- const RSA_ALGORITHM = 'RSA-OAEP';
8
- // RSA-OAEP
9
- class RSABrowserCrypter {
10
- genKeyPair(length = 2048) {
11
- return crypto.generateKey({
12
- name: RSA_ALGORITHM,
13
- modulusLength: length,
14
- publicExponent: new Uint8Array([1, 0, 1]),
15
- hash: 'SHA-256',
16
- }, true, ['encrypt', 'decrypt']);
17
- }
18
- async formatPublicKey(key) {
19
- const exported = await crypto.exportKey('spki', key);
20
- const base64 = window.btoa(ab2str(exported));
21
- return `-----BEGIN PUBLIC KEY-----\n${base64}\n-----END PUBLIC KEY-----`;
22
- }
23
- async encrypt(message, key) {
24
- const encrypted = await crypto.encrypt({ name: RSA_ALGORITHM }, key, new TextEncoder().encode(message));
25
- return toBase58(new Uint8Array(encrypted));
26
- }
27
- async decrypt(message, key) {
28
- const decrypted = await crypto.decrypt({ name: RSA_ALGORITHM }, key, fromBase58(message));
29
- return Buffer.from(new Uint8Array(decrypted)).toString('utf8');
30
- }
31
- }
32
- export default new RSABrowserCrypter();
33
- export { RSABrowserCrypter };
6
+ const RSA_ALGORITHM = "RSA-OAEP";
7
+ var RSABrowserCrypter = class {
8
+ genKeyPair(length = 2048) {
9
+ return crypto.generateKey({
10
+ name: RSA_ALGORITHM,
11
+ modulusLength: length,
12
+ publicExponent: new Uint8Array([
13
+ 1,
14
+ 0,
15
+ 1
16
+ ]),
17
+ hash: "SHA-256"
18
+ }, true, ["encrypt", "decrypt"]);
19
+ }
20
+ async formatPublicKey(key) {
21
+ const exported = await crypto.exportKey("spki", key);
22
+ return `-----BEGIN PUBLIC KEY-----\n${window.btoa(ab2str(exported))}\n-----END PUBLIC KEY-----`;
23
+ }
24
+ async encrypt(message, key) {
25
+ const encrypted = await crypto.encrypt({ name: RSA_ALGORITHM }, key, new TextEncoder().encode(message));
26
+ return toBase58(new Uint8Array(encrypted));
27
+ }
28
+ async decrypt(message, key) {
29
+ const decrypted = await crypto.decrypt({ name: RSA_ALGORITHM }, key, fromBase58(message));
30
+ return Buffer.from(new Uint8Array(decrypted)).toString("utf8");
31
+ }
32
+ };
33
+ var rsa_browserify_default = new RSABrowserCrypter();
34
+
35
+ //#endregion
36
+ export { RSABrowserCrypter, rsa_browserify_default as default };
@@ -1,11 +1,13 @@
1
- import crypto from 'crypto';
2
- import { BytesType, EncodingType } from '@ocap/util';
3
- import Crypter from '../protocols/crypter';
4
- declare class RSACrypter extends Crypter {
5
- genKeyPair(length?: number): crypto.KeyPairSyncResult<string, string>;
6
- encrypt(message: BytesType, key: string, encoding?: EncodingType): BytesType;
7
- decrypt(message: BytesType, key: string, encoding?: EncodingType): BytesType;
1
+ import _default$1 from "../protocols/crypter.js";
2
+ import { BytesType, EncodingType } from "@ocap/util";
3
+ import crypto from "crypto";
4
+
5
+ //#region src/crypter/rsa.d.ts
6
+ declare class RSACrypter extends _default$1 {
7
+ genKeyPair(length?: number): crypto.KeyPairSyncResult<string, string>;
8
+ encrypt(message: BytesType, key: string, encoding?: EncodingType): BytesType;
9
+ decrypt(message: BytesType, key: string, encoding?: EncodingType): BytesType;
8
10
  }
9
11
  declare const _default: RSACrypter;
10
- export default _default;
11
- export { RSACrypter };
12
+ //#endregion
13
+ export { RSACrypter, _default as default };
@@ -1,28 +1,31 @@
1
- import crypto from 'crypto';
2
- import { toBuffer } from '@ocap/util';
3
- import Crypter from '../protocols/crypter';
4
- import { encode } from '../encode';
5
- // RSA-OAEP
6
- class RSACrypter extends Crypter {
7
- genKeyPair(length = 2048) {
8
- return crypto.generateKeyPairSync('rsa', {
9
- modulusLength: length,
10
- publicKeyEncoding: {
11
- type: 'spki',
12
- format: 'pem',
13
- },
14
- privateKeyEncoding: {
15
- type: 'pkcs8',
16
- format: 'pem',
17
- },
18
- });
19
- }
20
- encrypt(message, key, encoding = 'hex') {
21
- return encode(crypto.publicEncrypt(key, toBuffer(message)), encoding);
22
- }
23
- decrypt(message, key, encoding = 'hex') {
24
- return encode(crypto.privateDecrypt(key, toBuffer(message)), encoding);
25
- }
26
- }
27
- export default new RSACrypter();
28
- export { RSACrypter };
1
+ import { encode } from "../encode.js";
2
+ import crypter_default from "../protocols/crypter.js";
3
+ import { toBuffer } from "@ocap/util";
4
+ import crypto from "crypto";
5
+
6
+ //#region src/crypter/rsa.ts
7
+ var RSACrypter = class extends crypter_default {
8
+ genKeyPair(length = 2048) {
9
+ return crypto.generateKeyPairSync("rsa", {
10
+ modulusLength: length,
11
+ publicKeyEncoding: {
12
+ type: "spki",
13
+ format: "pem"
14
+ },
15
+ privateKeyEncoding: {
16
+ type: "pkcs8",
17
+ format: "pem"
18
+ }
19
+ });
20
+ }
21
+ encrypt(message, key, encoding = "hex") {
22
+ return encode(crypto.publicEncrypt(key, toBuffer(message)), encoding);
23
+ }
24
+ decrypt(message, key, encoding = "hex") {
25
+ return encode(crypto.privateDecrypt(key, toBuffer(message)), encoding);
26
+ }
27
+ };
28
+ var rsa_default = new RSACrypter();
29
+
30
+ //#endregion
31
+ export { RSACrypter, rsa_default as default };
package/esm/encode.d.ts CHANGED
@@ -1,8 +1,12 @@
1
- import { BytesType, EncodingType } from '@ocap/util';
2
- export declare function encode(data: BytesType, encoding?: 'hex'): string;
3
- export declare function encode(data: BytesType, encoding?: 'base16'): string;
4
- export declare function encode(data: BytesType, encoding?: 'base58'): string;
5
- export declare function encode(data: BytesType, encoding?: 'base64'): string;
6
- export declare function encode(data: BytesType, encoding?: 'buffer'): Buffer;
7
- export declare function encode(data: BytesType, encoding?: 'Uint8Array'): Uint8Array;
8
- export declare function encode(data: BytesType, encoding?: EncodingType): BytesType;
1
+ import { BytesType, EncodingType } from "@ocap/util";
2
+
3
+ //#region src/encode.d.ts
4
+ declare function encode(data: BytesType, encoding?: 'hex'): string;
5
+ declare function encode(data: BytesType, encoding?: 'base16'): string;
6
+ declare function encode(data: BytesType, encoding?: 'base58'): string;
7
+ declare function encode(data: BytesType, encoding?: 'base64'): string;
8
+ declare function encode(data: BytesType, encoding?: 'buffer'): Buffer;
9
+ declare function encode(data: BytesType, encoding?: 'Uint8Array'): Uint8Array;
10
+ declare function encode(data: BytesType, encoding?: EncodingType): BytesType;
11
+ //#endregion
12
+ export { encode };
package/esm/encode.js CHANGED
@@ -1,19 +1,14 @@
1
- import { toHex, toUint8Array, toBase58, toBase64, toBuffer } from '@ocap/util';
2
- export function encode(data, encoding = 'hex') {
3
- if (['hex', 'base16'].includes(encoding)) {
4
- return toHex(data);
5
- }
6
- if (encoding === 'base58') {
7
- return toBase58(data);
8
- }
9
- if (encoding === 'base64') {
10
- return toBase64(data);
11
- }
12
- if (encoding === 'Uint8Array') {
13
- return toUint8Array(data);
14
- }
15
- if (encoding === 'buffer') {
16
- return toBuffer(data);
17
- }
18
- return data;
1
+ import { toBase58, toBase64, toBuffer, toHex, toUint8Array } from "@ocap/util";
2
+
3
+ //#region src/encode.ts
4
+ function encode(data, encoding = "hex") {
5
+ if (["hex", "base16"].includes(encoding)) return toHex(data);
6
+ if (encoding === "base58") return toBase58(data);
7
+ if (encoding === "base64") return toBase64(data);
8
+ if (encoding === "Uint8Array") return toUint8Array(data);
9
+ if (encoding === "buffer") return toBuffer(data);
10
+ return data;
19
11
  }
12
+
13
+ //#endregion
14
+ export { encode };
@@ -1,13 +1,16 @@
1
- import { BytesType, EncodingType } from '@ocap/util';
1
+ import { BytesType, EncodingType } from "@ocap/util";
2
+
3
+ //#region src/hasher/keccak.d.ts
4
+
2
5
  /**
3
6
  * Keccak support with different hash length
4
7
  *
5
8
  * @class KeccakHasher
6
9
  */
7
10
  declare class KeccakHasher {
8
- [x: string]: (data: BytesType, round?: number, encoding?: EncodingType) => BytesType;
9
- constructor();
11
+ [x: string]: (data: BytesType, round?: number, encoding?: EncodingType) => BytesType;
12
+ constructor();
10
13
  }
11
14
  declare const _default: KeccakHasher;
12
- export default _default;
13
- export { KeccakHasher };
15
+ //#endregion
16
+ export { KeccakHasher, _default as default };
@@ -1,37 +1,38 @@
1
- /* eslint-disable @typescript-eslint/ban-ts-comment */
2
- import sha3 from 'js-sha3';
3
- import { toUint8Array } from '@ocap/util';
4
- import { encode } from '../encode';
1
+ import { encode } from "../encode.js";
2
+ import { toUint8Array } from "@ocap/util";
3
+ import sha3 from "js-sha3";
4
+
5
+ //#region src/hasher/keccak.ts
5
6
  /**
6
- * Keccak support with different hash length
7
- *
8
- * @class KeccakHasher
9
- */
10
- class KeccakHasher {
11
- constructor() {
12
- [224, 256, 384, 512].forEach((x) => {
13
- const name = `hash${x}`;
14
- // @ts-ignore
15
- const hasher = (data) => sha3[`keccak${x}`](data);
16
- const hashFn = (input, round) => {
17
- if (round === 1) {
18
- return hasher(input);
19
- }
20
- return hashFn(hasher(input), round - 1);
21
- };
22
- this[name] = (data, round = 1, encoding = 'hex') => {
23
- let input = data;
24
- try {
25
- input = toUint8Array(data);
26
- }
27
- catch (err) {
28
- // Do nothing
29
- }
30
- const res = hashFn(input, round);
31
- return encode(`0x${res}`, encoding);
32
- };
33
- });
34
- }
35
- }
36
- export default new KeccakHasher();
37
- export { KeccakHasher };
7
+ * Keccak support with different hash length
8
+ *
9
+ * @class KeccakHasher
10
+ */
11
+ var KeccakHasher = class {
12
+ constructor() {
13
+ [
14
+ 224,
15
+ 256,
16
+ 384,
17
+ 512
18
+ ].forEach((x) => {
19
+ const name = `hash${x}`;
20
+ const hasher = (data) => sha3[`keccak${x}`](data);
21
+ const hashFn = (input, round) => {
22
+ if (round === 1) return hasher(input);
23
+ return hashFn(hasher(input), round - 1);
24
+ };
25
+ this[name] = (data, round = 1, encoding = "hex") => {
26
+ let input = data;
27
+ try {
28
+ input = toUint8Array(data);
29
+ } catch (err) {}
30
+ return encode(`0x${hashFn(input, round)}`, encoding);
31
+ };
32
+ });
33
+ }
34
+ };
35
+ var keccak_default = new KeccakHasher();
36
+
37
+ //#endregion
38
+ export { KeccakHasher, keccak_default as default };
@@ -1,13 +1,16 @@
1
- import { BytesType, EncodingType } from '@ocap/util';
1
+ import { BytesType, EncodingType } from "@ocap/util";
2
+
3
+ //#region src/hasher/sha2.d.ts
4
+
2
5
  /**
3
6
  * Sha2 support with different hash length
4
7
  *
5
8
  * @class
6
9
  */
7
10
  declare class Sha2Hasher {
8
- [x: string]: (data: BytesType, round?: number, encoding?: EncodingType) => BytesType;
9
- constructor();
11
+ [x: string]: (data: BytesType, round?: number, encoding?: EncodingType) => BytesType;
12
+ constructor();
10
13
  }
11
14
  declare const _default: Sha2Hasher;
12
- export default _default;
13
- export { Sha2Hasher };
15
+ //#endregion
16
+ export { Sha2Hasher, _default as default };
@@ -1,43 +1,45 @@
1
- /* eslint-disable @typescript-eslint/ban-ts-comment */
2
- import { toUint8Array } from '@ocap/util';
3
- import { sha224, sha256, sha384, sha512 } from 'hash.js';
4
- import { encode } from '../encode';
1
+ import { encode } from "../encode.js";
2
+ import { toUint8Array } from "@ocap/util";
3
+ import _hash_js from "hash.js";
4
+ const { sha224, sha256, sha384, sha512 } = _hash_js;
5
+
6
+ //#region src/hasher/sha2.ts
5
7
  const hashFns = {
6
- sha224,
7
- sha256,
8
- sha384,
9
- sha512,
8
+ sha224,
9
+ sha256,
10
+ sha384,
11
+ sha512
10
12
  };
11
13
  /**
12
- * Sha2 support with different hash length
13
- *
14
- * @class
15
- */
16
- class Sha2Hasher {
17
- constructor() {
18
- [224, 256, 384, 512].forEach((x) => {
19
- const name = `hash${x}`;
20
- // @ts-ignore
21
- const hasher = hashFns[`sha${x}`];
22
- const hashFn = (input, round) => {
23
- let inputBytes = input;
24
- try {
25
- inputBytes = toUint8Array(input);
26
- }
27
- catch (err) {
28
- // Do nothing
29
- }
30
- if (round === 1) {
31
- return `0x${hasher().update(inputBytes).digest('hex')}`;
32
- }
33
- return hashFn(hashFn(inputBytes, 1), round - 1);
34
- };
35
- this[name] = (data, round = 2, encoding = 'hex') => {
36
- const res = hashFn(data, round);
37
- return encode(res, encoding);
38
- };
39
- });
40
- }
41
- }
42
- export default new Sha2Hasher();
43
- export { Sha2Hasher };
14
+ * Sha2 support with different hash length
15
+ *
16
+ * @class
17
+ */
18
+ var Sha2Hasher = class {
19
+ constructor() {
20
+ [
21
+ 224,
22
+ 256,
23
+ 384,
24
+ 512
25
+ ].forEach((x) => {
26
+ const name = `hash${x}`;
27
+ const hasher = hashFns[`sha${x}`];
28
+ const hashFn = (input, round) => {
29
+ let inputBytes = input;
30
+ try {
31
+ inputBytes = toUint8Array(input);
32
+ } catch (err) {}
33
+ if (round === 1) return `0x${hasher().update(inputBytes).digest("hex")}`;
34
+ return hashFn(hashFn(inputBytes, 1), round - 1);
35
+ };
36
+ this[name] = (data, round = 2, encoding = "hex") => {
37
+ return encode(hashFn(data, round), encoding);
38
+ };
39
+ });
40
+ }
41
+ };
42
+ var sha2_default = new Sha2Hasher();
43
+
44
+ //#endregion
45
+ export { Sha2Hasher, sha2_default as default };
@@ -1,13 +1,16 @@
1
- import { BytesType, EncodingType } from '@ocap/util';
1
+ import { BytesType, EncodingType } from "@ocap/util";
2
+
3
+ //#region src/hasher/sha3.d.ts
4
+
2
5
  /**
3
6
  * Sha3 support with different hash length
4
7
  *
5
8
  * @class Sha3Hasher
6
9
  */
7
10
  declare class Sha3Hasher {
8
- [x: string]: (data: BytesType, round?: number, encoding?: EncodingType) => BytesType;
9
- constructor();
11
+ [x: string]: (data: BytesType, round?: number, encoding?: EncodingType) => BytesType;
12
+ constructor();
10
13
  }
11
14
  declare const _default: Sha3Hasher;
12
- export default _default;
13
- export { Sha3Hasher };
15
+ //#endregion
16
+ export { Sha3Hasher, _default as default };