@oazmi/superbuild 0.1.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 (118) hide show
  1. package/esm/_dnt.shims.d.ts +2 -0
  2. package/esm/_dnt.shims.d.ts.map +1 -0
  3. package/esm/_dnt.shims.js +57 -0
  4. package/esm/deps/jsr.io/@oazmi/esbuild-types/0.28.0/src/mod.d.ts +598 -0
  5. package/esm/deps/jsr.io/@oazmi/esbuild-types/0.28.0/src/mod.d.ts.map +1 -0
  6. package/esm/deps/jsr.io/@oazmi/esbuild-types/0.28.0/src/mod.js +1 -0
  7. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/alias.d.ts +617 -0
  8. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/alias.d.ts.map +1 -0
  9. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/alias.js +671 -0
  10. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/array1d.d.ts +487 -0
  11. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/array1d.d.ts.map +1 -0
  12. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/array1d.js +536 -0
  13. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/array2d.d.ts +266 -0
  14. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/array2d.d.ts.map +1 -0
  15. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/array2d.js +318 -0
  16. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/binder.d.ts +368 -0
  17. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/binder.d.ts.map +1 -0
  18. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/binder.js +380 -0
  19. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/crossenv.d.ts +711 -0
  20. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/crossenv.d.ts.map +1 -0
  21. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/crossenv.js +1173 -0
  22. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/cryptoman.d.ts +171 -0
  23. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/cryptoman.d.ts.map +1 -0
  24. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/cryptoman.js +308 -0
  25. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/deps.d.ts +10 -0
  26. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/deps.d.ts.map +1 -0
  27. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/deps.js +10 -0
  28. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/eightpack.d.ts +168 -0
  29. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/eightpack.d.ts.map +1 -0
  30. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/eightpack.js +236 -0
  31. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/eightpack_varint.d.ts +90 -0
  32. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/eightpack_varint.d.ts.map +1 -0
  33. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/eightpack_varint.js +237 -0
  34. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/numericarray.d.ts +213 -0
  35. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/numericarray.d.ts.map +1 -0
  36. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/numericarray.js +363 -0
  37. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/numericmethods.d.ts +233 -0
  38. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/numericmethods.d.ts.map +1 -0
  39. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/numericmethods.js +256 -0
  40. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/pathman.d.ts +1436 -0
  41. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/pathman.d.ts.map +1 -0
  42. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/pathman.js +1730 -0
  43. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/promiseman.d.ts +647 -0
  44. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/promiseman.d.ts.map +1 -0
  45. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/promiseman.js +762 -0
  46. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/stringman.d.ts +538 -0
  47. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/stringman.d.ts.map +1 -0
  48. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/stringman.js +626 -0
  49. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/struct.d.ts +734 -0
  50. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/struct.d.ts.map +1 -0
  51. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/struct.js +764 -0
  52. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/typedbuffer.d.ts +137 -0
  53. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/typedbuffer.d.ts.map +1 -0
  54. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/typedbuffer.js +234 -0
  55. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/typedefs.d.ts +391 -0
  56. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/typedefs.d.ts.map +1 -0
  57. package/esm/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/typedefs.js +8 -0
  58. package/esm/deps.d.ts +56 -0
  59. package/esm/deps.d.ts.map +1 -0
  60. package/esm/deps.js +37 -0
  61. package/esm/esbuild/metafile.d.ts +90 -0
  62. package/esm/esbuild/metafile.d.ts.map +1 -0
  63. package/esm/esbuild/metafile.js +275 -0
  64. package/esm/esbuild/native.d.ts +95 -0
  65. package/esm/esbuild/native.d.ts.map +1 -0
  66. package/esm/esbuild/native.js +127 -0
  67. package/esm/esbuild/outputfile.d.ts +86 -0
  68. package/esm/esbuild/outputfile.d.ts.map +1 -0
  69. package/esm/esbuild/outputfile.js +221 -0
  70. package/esm/esbuild/strongtypes.d.ts +90 -0
  71. package/esm/esbuild/strongtypes.d.ts.map +1 -0
  72. package/esm/esbuild/strongtypes.js +8 -0
  73. package/esm/esbuild/typedefs.d.ts +121 -0
  74. package/esm/esbuild/typedefs.d.ts.map +1 -0
  75. package/esm/esbuild/typedefs.js +55 -0
  76. package/esm/esbuild/weaktypes.d.ts +46 -0
  77. package/esm/esbuild/weaktypes.d.ts.map +1 -0
  78. package/esm/esbuild/weaktypes.js +8 -0
  79. package/esm/funcdefs.d.ts +89 -0
  80. package/esm/funcdefs.d.ts.map +1 -0
  81. package/esm/funcdefs.js +145 -0
  82. package/esm/mod.d.ts +4 -0
  83. package/esm/mod.d.ts.map +1 -0
  84. package/esm/mod.js +2 -0
  85. package/esm/package.json +3 -0
  86. package/esm/plugins/emissions_driver.d.ts +33 -0
  87. package/esm/plugins/emissions_driver.d.ts.map +1 -0
  88. package/esm/plugins/emissions_driver.js +260 -0
  89. package/esm/plugins/long_build.d.ts +138 -0
  90. package/esm/plugins/long_build.d.ts.map +1 -0
  91. package/esm/plugins/long_build.js +288 -0
  92. package/esm/plugins/native_replica.d.ts +48 -0
  93. package/esm/plugins/native_replica.d.ts.map +1 -0
  94. package/esm/plugins/native_replica.js +122 -0
  95. package/esm/super/build.d.ts +53 -0
  96. package/esm/super/build.d.ts.map +1 -0
  97. package/esm/super/build.js +39 -0
  98. package/esm/super/build_context.d.ts +83 -0
  99. package/esm/super/build_context.d.ts.map +1 -0
  100. package/esm/super/build_context.js +124 -0
  101. package/esm/super/mod.d.ts +12 -0
  102. package/esm/super/mod.d.ts.map +1 -0
  103. package/esm/super/mod.js +9 -0
  104. package/esm/super/plugin.d.ts +23 -0
  105. package/esm/super/plugin.d.ts.map +1 -0
  106. package/esm/super/plugin.js +31 -0
  107. package/esm/super/plugin_build.d.ts +38 -0
  108. package/esm/super/plugin_build.d.ts.map +1 -0
  109. package/esm/super/plugin_build.js +198 -0
  110. package/esm/super/typedefs.d.ts +323 -0
  111. package/esm/super/typedefs.d.ts.map +1 -0
  112. package/esm/super/typedefs.js +10 -0
  113. package/esm/typedefs.d.ts +21 -0
  114. package/esm/typedefs.d.ts.map +1 -0
  115. package/esm/typedefs.js +5 -0
  116. package/license.md +37 -0
  117. package/package.json +88 -0
  118. package/readme.md +181 -0
@@ -0,0 +1,171 @@
1
+ /** utility functions for cryptography.
2
+ *
3
+ * > [!note]
4
+ * > some functions require the use of the built-in `crypto.subtle` functions, which not available in client-side "http" contexts.
5
+ * > your client will have to connect via "https" for their browser to let them use `crypto.subtle` hashing algorithms.
6
+ *
7
+ * @module
8
+ */
9
+ /** generate a CRC32 hash table for quick lookups. */
10
+ export declare const createCrc32Table: () => Int32Array;
11
+ /** the CRC32 hash is quick to compute and used frequently in compression functions and their derivatives.
12
+ *
13
+ * you do not have to provide the `bytes` array in its entirety all at once,
14
+ * because you can continue off with the previous partial byte array's crc-hash using the second argument.
15
+ *
16
+ * @param bytes an array of bytes to compute the hash for. can be any kind of array, so long as all byte numbers conform to being unsigned integers that do not exceed the maximum value of `255` (8-bit max value)
17
+ * @param crc provide any previous crc hash that you'd like to continue from, or leave it `undefined` to begin from the standard value of `0xFFFFFFFF` by default
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import { assertEquals } from "jsr:@std/assert"
22
+ *
23
+ * const
24
+ * txtenc = new TextEncoder(),
25
+ * crc_a = crc32(txtenc.encode("hello ")), // == 0xED81F9F6
26
+ * crc_b = crc32(txtenc.encode("world"), crc_a), // == 0x0D4A1185
27
+ * crc_c = crc32(txtenc.encode("hello world")) // == 0x0D4A1185
28
+ * assertEquals(crc_b, crc_c)
29
+ * ```
30
+ */
31
+ export declare const crc32: (bytes: Uint8Array | Array<number>, crc?: number) => number;
32
+ /** clamps a 32-byte `bigint` private key (scalar), per `x25519`'s requirement, and then returns it back. */
33
+ export declare const clampX25519Scalar: (scalar: bigint) => bigint;
34
+ /** decode a little-endian based bytes representation of a number to a `bigint`. */
35
+ export declare const decodeBigintL: (bytes: Uint8Array) => bigint;
36
+ /** encode a `bigint` into an array of little-endian bytes of either a fixed `length`,
37
+ * or a variable length (i.e. whatever suffices to hold the bigint's info).
38
+ */
39
+ export declare const encodeBigintL: (big_value: bigint, length?: number) => Uint8Array<ArrayBuffer>;
40
+ /** `curve25519` scalar multiplication (via montgomery-ladder), implementing [`RFC7748`](https://datatracker.ietf.org/doc/html/rfc7748).
41
+ *
42
+ * the implementation follows the pseudo code presented in
43
+ * [wikipedia](https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Constant_time_Montgomery_ladder).
44
+ *
45
+ * @param scalar your private key (must be clamped beforehand when being used for `x25519` key generation).
46
+ * @param basepoint the basepoint "common secret".
47
+ * this is typically `9n` for `x25519` key generation, and hence it is also the default value.
48
+ * @returns the "public key" as a bigint.
49
+ *
50
+ * @example
51
+ * an "alice and bob" example but with bob replaced with "bobby", so that both names are 5 characters long.
52
+ *
53
+ * ```ts
54
+ * import { assertEquals } from "jsr:@std/assert"
55
+ *
56
+ * // the private keys of both parties
57
+ * const
58
+ * alice_private_key = 123n,
59
+ * bobby_private_key = 789n
60
+ *
61
+ * // both compute a public key, based on a globally agreed upon basekey (global common secret).
62
+ * // this "globally common secret" is `9n` for the `x25519` key-generation.
63
+ * const
64
+ * alice_public_key = curve25519ScalarMult(alice_private_key, 9n),
65
+ * bobby_public_key = curve25519ScalarMult(bobby_private_key, 9n)
66
+ *
67
+ * assertEquals(alice_public_key, 36775675751433867979441935675960806006270981998619265997805716105314274742222n)
68
+ * assertEquals(bobby_public_key, 4776225643423455257904064614728753759233983901518081694545578805283024213294n)
69
+ *
70
+ * // now, they both exchange their public keys, and then use it as the "basepoint" (a secret between the two),
71
+ * // to _derive_ a _common shared_ secret key, that is the same for both of them.
72
+ * const
73
+ * alice_shared_secret_key = curve25519ScalarMult(alice_private_key, bobby_public_key),
74
+ * bobby_shared_secret_key = curve25519ScalarMult(bobby_private_key, alice_public_key)
75
+ *
76
+ * assertEquals(alice_shared_secret_key, bobby_shared_secret_key)
77
+ * assertEquals(alice_shared_secret_key, 26692159771081237073471702917999531026498379047472371972701945697203840355541n)
78
+ *
79
+ * // notice that neither alice nor bobby had to exchange this secret key with one another;
80
+ * // they both just computed the same value due to the symmetric nature of `curve25519`.
81
+ * // if a third person, say cluky, were to intercept both public keys,
82
+ * // they won't be able to derive the same secret value that's between alice and bobby, no matter what they try,
83
+ * // unless they manage to get their hands on either one's private key.
84
+ * //
85
+ * // and so now, both alice and bobby can safely use this secret key to encrypt and decrypt each other's messages.
86
+ * // there are many encryption protocols, but a few common ones are: AES256, ChaCha20, HMAC, and the list goes.
87
+ * ```
88
+ */
89
+ export declare const curve25519ScalarMult: (scalar: bigint, basepoint?: bigint) => bigint;
90
+ export interface KeyPair {
91
+ privateKey: Uint8Array;
92
+ publicKey: Uint8Array;
93
+ }
94
+ /** generate an `x25519` private and public keypair (in 32-byte array form) using the `curve25519` scalar multiplication algorithm.
95
+ *
96
+ * you can optionally provide an existing `private_key`, either as a `bigint`,
97
+ * or as a 32-byte array (`Uint8Array` of size `32`).
98
+ * if your private key is not pre-clamped based on `x25519`'s requirements,
99
+ * it will be forcefully clampped, and the returned {@link KeyPair.privateKey} will be clampped.
100
+ *
101
+ * if you don't provide a private key, this function will randomly generate one for you,
102
+ * using: `crypto.getRandomValues(new Uint8Array(32))`.
103
+ *
104
+ * > [!note]
105
+ * > to convert the key pairs to base64 strings,
106
+ * > use the freshly added (es2025+) `Uint8Array.toBase64` method on each of the keys.
107
+ */
108
+ export declare const generateX25519Keypair: (private_key?: bigint | Uint8Array) => KeyPair;
109
+ /** generate a `x25519` secret key that is common between your private key, and your peer's public key.
110
+ *
111
+ * > [!note]
112
+ * > your private key is NOT clampped here! it should already be clampped if it we generated via some `x25519` keygen.
113
+ *
114
+ * @example
115
+ * an "alice and bob" example but with bob replaced with "bobby", so that both names are 5 characters long.
116
+ *
117
+ * ```ts
118
+ * import { assertEquals } from "jsr:@std/assert"
119
+ *
120
+ * // the private keys of both parties (both are incorrectly clampped for `x25519`, but we'll just go with it)
121
+ * const
122
+ * alice_private_key = 123n,
123
+ * bobby_private_key = 789n
124
+ *
125
+ * // both compute a public key, based on a globally agreed upon basekey (global common secret).
126
+ * // this "globally common secret" is `9n` for the `x25519` key-generation.
127
+ * const
128
+ * alice_public_key: Uint8Array = generateX25519SecretKey(alice_private_key, 9n),
129
+ * bobby_public_key: Uint8Array = generateX25519SecretKey(bobby_private_key, 9n)
130
+ *
131
+ * // now, they both exchange their public keys, and then use it as the "basepoint" (a secret between the two),
132
+ * // to _derive_ a _common shared_ secret key, that is the same for both of them.
133
+ * const
134
+ * alice_shared_secret_key: Uint8Array = generateX25519SecretKey(alice_private_key, bobby_public_key),
135
+ * bobby_shared_secret_key: Uint8Array = generateX25519SecretKey(bobby_private_key, alice_public_key)
136
+ *
137
+ * assertEquals(alice_shared_secret_key, bobby_shared_secret_key)
138
+ *
139
+ * // notice that neither alice nor bobby had to exchange this secret key with one another;
140
+ * // they both just computed the same value due to the symmetric nature of `curve25519`.
141
+ * // if a third person, say cluky, were to intercept both public keys,
142
+ * // they won't be able to derive the same secret value that's between alice and bobby, no matter what they try,
143
+ * // unless they manage to get their hands on either one's private key.
144
+ * //
145
+ * // and so now, both alice and bobby can safely use this secret key to encrypt and decrypt each other's messages.
146
+ * // there are many encryption protocols, but a few common ones are: AES256, ChaCha20, HMAC, and the list goes.
147
+ * ```
148
+ */
149
+ export declare const generateX25519SecretKey: (your_private_key: bigint | Uint8Array, peer_public_key: bigint | Uint8Array) => Uint8Array<ArrayBuffer>;
150
+ /** SHA-256 hash function.
151
+ *
152
+ * > [!important]
153
+ * > requires `crypto.subtle` to be available in your client's browser, which is only available in `https` sites.
154
+ */
155
+ export declare const sha256: (message: string | ArrayBuffer) => Promise<ArrayBuffer>;
156
+ /** HMAC-SHA256 function.
157
+ *
158
+ * > [!important]
159
+ * > requires `crypto.subtle` to be available in your client's browser, which is only available in `https` sites.
160
+ */
161
+ export declare const hmacSha256: (encryption_key: string | ArrayBuffer, message: string | ArrayBuffer) => Promise<ArrayBuffer>;
162
+ /** apply the {@link hmacSha256} hashing function recursively on multiple messages/binaries.
163
+ *
164
+ * the first message will be used as the encryption key for the second message,
165
+ * and the resulting encrypted message will be used as the encryption key for the third message,
166
+ * and so on, until the final message is encrypted and returned.
167
+ *
168
+ * note that you should provide at least two messages.
169
+ */
170
+ export declare const hmacSha256Recursive: (message1: string | ArrayBuffer, message2: string | ArrayBuffer, ...rest_of_messages: (string | ArrayBuffer)[]) => Promise<ArrayBuffer>;
171
+ //# sourceMappingURL=cryptoman.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cryptoman.d.ts","sourceRoot":"","sources":["../../../../../../../src/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/cryptoman.ts"],"names":[],"mappings":"AAAA;;;;;;;EAOE;AAMF,qDAAqD;AACrD,eAAO,MAAM,gBAAgB,QAAO,UAanC,CAAA;AAID;;;;;;;;;;;;;;;;;;;EAmBE;AACF,eAAO,MAAM,KAAK,UAAW,UAAU,GAAG,KAAK,CAAC,MAAM,CAAC,QAAQ,MAAM,WAOpE,CAAA;AAED,4GAA4G;AAC5G,eAAO,MAAM,iBAAiB,WAAY,MAAM,KAAG,MAWlD,CAAA;AAED,mFAAmF;AACnF,eAAO,MAAM,aAAa,UAAW,UAAU,KAAG,MAOjD,CAAA;AAED;;EAEE;AACF,eAAO,MAAM,aAAa,cAAe,MAAM,WAAW,MAAM,KAAG,UAAU,CAAC,WAAW,CAiBxF,CAAA;AAuBD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAgDE;AACF,eAAO,MAAM,oBAAoB,WAAY,MAAM,cAAa,MAAM,KAAQ,MAmD7E,CAAA;AAED,MAAM,WAAW,OAAO;IACvB,UAAU,EAAE,UAAU,CAAA;IACtB,SAAS,EAAE,UAAU,CAAA;CACrB;AAED;;;;;;;;;;;;;EAaE;AACF,eAAO,MAAM,qBAAqB,iBAAkB,MAAM,GAAG,UAAU,KAAG,OAYzE,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAuCE;AACF,eAAO,MAAM,uBAAuB,qBACjB,MAAM,GAAG,UAAU,mBACpB,MAAM,GAAG,UAAU,KAClC,UAAU,CAAC,WAAW,CASxB,CAAA;AAID;;;;EAIE;AACF,eAAO,MAAM,MAAM,YAAmB,MAAM,GAAG,WAAW,KAAG,OAAO,CAAC,WAAW,CAK/E,CAAA;AAED;;;;EAIE;AACF,eAAO,MAAM,UAAU,mBACN,MAAM,GAAG,WAAW,WAC3B,MAAM,GAAG,WAAW,KAC3B,OAAO,CAAC,WAAW,CAOrB,CAAA;AAED;;;;;;;EAOE;AACF,eAAO,MAAM,mBAAmB,qHAI5B,OAAO,CAAC,WAAW,CAMtB,CAAA"}
@@ -0,0 +1,308 @@
1
+ /** utility functions for cryptography.
2
+ *
3
+ * > [!note]
4
+ * > some functions require the use of the built-in `crypto.subtle` functions, which not available in client-side "http" contexts.
5
+ * > your client will have to connect via "https" for their browser to let them use `crypto.subtle` hashing algorithms.
6
+ *
7
+ * @module
8
+ */
9
+ import { textEncoder } from "./eightpack.js";
10
+ import { isBigint } from "./struct.js";
11
+ /** generate a CRC32 hash table for quick lookups. */
12
+ export const createCrc32Table = () => {
13
+ const polynomial = -306674912, crc32_table = new Int32Array(256);
14
+ for (let i = 0; i < 256; i++) {
15
+ // initialize the table with `polynomial` being the starting seed
16
+ let r = i;
17
+ for (let bit = 8; bit > 0; --bit) {
18
+ r = ((r & 1) ? ((r >>> 1) ^ polynomial) : (r >>> 1));
19
+ }
20
+ crc32_table[i] = r;
21
+ }
22
+ return crc32_table;
23
+ };
24
+ let crc32_table;
25
+ /** the CRC32 hash is quick to compute and used frequently in compression functions and their derivatives.
26
+ *
27
+ * you do not have to provide the `bytes` array in its entirety all at once,
28
+ * because you can continue off with the previous partial byte array's crc-hash using the second argument.
29
+ *
30
+ * @param bytes an array of bytes to compute the hash for. can be any kind of array, so long as all byte numbers conform to being unsigned integers that do not exceed the maximum value of `255` (8-bit max value)
31
+ * @param crc provide any previous crc hash that you'd like to continue from, or leave it `undefined` to begin from the standard value of `0xFFFFFFFF` by default
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * import { assertEquals } from "jsr:@std/assert"
36
+ *
37
+ * const
38
+ * txtenc = new TextEncoder(),
39
+ * crc_a = crc32(txtenc.encode("hello ")), // == 0xED81F9F6
40
+ * crc_b = crc32(txtenc.encode("world"), crc_a), // == 0x0D4A1185
41
+ * crc_c = crc32(txtenc.encode("hello world")) // == 0x0D4A1185
42
+ * assertEquals(crc_b, crc_c)
43
+ * ```
44
+ */
45
+ export const crc32 = (bytes, crc) => {
46
+ crc = crc === undefined ? 0xFFFFFFFF : crc ^ -1;
47
+ crc32_table ??= createCrc32Table();
48
+ for (let i = 0; i < bytes.length; ++i) {
49
+ crc = crc32_table[(crc ^ bytes[i]) & 0xFF] ^ (crc >>> 8);
50
+ }
51
+ return (crc ^ -1) >>> 0;
52
+ };
53
+ /** clamps a 32-byte `bigint` private key (scalar), per `x25519`'s requirement, and then returns it back. */
54
+ export const clampX25519Scalar = (scalar) => {
55
+ const mask_bits = 32n * 8n, mask = (1n << (mask_bits - 1n)) - 1n;
56
+ // clear the 3 lower bits
57
+ scalar = (scalar >> 3n) << 3n;
58
+ // clear the highest bit on the 32-byte mark
59
+ scalar &= mask;
60
+ // set second-highest bit
61
+ scalar |= 1n << (mask_bits - 2n);
62
+ return scalar;
63
+ };
64
+ /** decode a little-endian based bytes representation of a number to a `bigint`. */
65
+ export const decodeBigintL = (bytes) => {
66
+ // accumulating byte in reverse order, since the last byte in little-endian is the most significant byte.
67
+ let acc = 0n;
68
+ for (let i = bytes.length - 1; i >= 0; i--) {
69
+ acc = ((acc << 8n) + BigInt(bytes[i]));
70
+ }
71
+ return acc;
72
+ };
73
+ /** encode a `bigint` into an array of little-endian bytes of either a fixed `length`,
74
+ * or a variable length (i.e. whatever suffices to hold the bigint's info).
75
+ */
76
+ export const encodeBigintL = (big_value, length) => {
77
+ if (length === undefined) {
78
+ length = 0;
79
+ let remainder = big_value;
80
+ while (remainder > 0n) {
81
+ remainder /= 0xffn;
82
+ length++;
83
+ }
84
+ }
85
+ const bytes = new Uint8Array(length);
86
+ for (let i = 0; i < length; i++) {
87
+ // acquire the current least significant numeric byte's value.
88
+ bytes[i] = Number(big_value & 0xffn);
89
+ // right-shift the bigint's value by 1 byte (8 bits).
90
+ big_value >>= 8n;
91
+ }
92
+ return bytes;
93
+ };
94
+ /** modular exponentiation (`base ** expo mod modulo`) */
95
+ const modExp = (base, expo, modulo) => {
96
+ let result = 1n;
97
+ let b = base % modulo;
98
+ let e = expo;
99
+ while (e > 0) {
100
+ if ((e & 1n) === 1n)
101
+ result = (result * b) % modulo;
102
+ b = (b * b) % modulo;
103
+ e >>= 1n;
104
+ }
105
+ return result;
106
+ };
107
+ const
108
+ // the `curve25519` field modulus is the prime number: `2^255 - 19`. I don't understand its significance.
109
+ P = (1n << 255n) - 19n,
110
+ // `a24` is defined as `a24 = (A + 2)/4`, where `A` is a parameter of the underlying montgomery curve - wikipedia.
111
+ // what the heck that means is of no concern to me.
112
+ // all I know is that `A = 486662` (from [here](https://en.wikipedia.org/wiki/Curve25519)).
113
+ a24 = (486662n - 2n) / 4n;
114
+ /** `curve25519` scalar multiplication (via montgomery-ladder), implementing [`RFC7748`](https://datatracker.ietf.org/doc/html/rfc7748).
115
+ *
116
+ * the implementation follows the pseudo code presented in
117
+ * [wikipedia](https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Constant_time_Montgomery_ladder).
118
+ *
119
+ * @param scalar your private key (must be clamped beforehand when being used for `x25519` key generation).
120
+ * @param basepoint the basepoint "common secret".
121
+ * this is typically `9n` for `x25519` key generation, and hence it is also the default value.
122
+ * @returns the "public key" as a bigint.
123
+ *
124
+ * @example
125
+ * an "alice and bob" example but with bob replaced with "bobby", so that both names are 5 characters long.
126
+ *
127
+ * ```ts
128
+ * import { assertEquals } from "jsr:@std/assert"
129
+ *
130
+ * // the private keys of both parties
131
+ * const
132
+ * alice_private_key = 123n,
133
+ * bobby_private_key = 789n
134
+ *
135
+ * // both compute a public key, based on a globally agreed upon basekey (global common secret).
136
+ * // this "globally common secret" is `9n` for the `x25519` key-generation.
137
+ * const
138
+ * alice_public_key = curve25519ScalarMult(alice_private_key, 9n),
139
+ * bobby_public_key = curve25519ScalarMult(bobby_private_key, 9n)
140
+ *
141
+ * assertEquals(alice_public_key, 36775675751433867979441935675960806006270981998619265997805716105314274742222n)
142
+ * assertEquals(bobby_public_key, 4776225643423455257904064614728753759233983901518081694545578805283024213294n)
143
+ *
144
+ * // now, they both exchange their public keys, and then use it as the "basepoint" (a secret between the two),
145
+ * // to _derive_ a _common shared_ secret key, that is the same for both of them.
146
+ * const
147
+ * alice_shared_secret_key = curve25519ScalarMult(alice_private_key, bobby_public_key),
148
+ * bobby_shared_secret_key = curve25519ScalarMult(bobby_private_key, alice_public_key)
149
+ *
150
+ * assertEquals(alice_shared_secret_key, bobby_shared_secret_key)
151
+ * assertEquals(alice_shared_secret_key, 26692159771081237073471702917999531026498379047472371972701945697203840355541n)
152
+ *
153
+ * // notice that neither alice nor bobby had to exchange this secret key with one another;
154
+ * // they both just computed the same value due to the symmetric nature of `curve25519`.
155
+ * // if a third person, say cluky, were to intercept both public keys,
156
+ * // they won't be able to derive the same secret value that's between alice and bobby, no matter what they try,
157
+ * // unless they manage to get their hands on either one's private key.
158
+ * //
159
+ * // and so now, both alice and bobby can safely use this secret key to encrypt and decrypt each other's messages.
160
+ * // there are many encryption protocols, but a few common ones are: AES256, ChaCha20, HMAC, and the list goes.
161
+ * ```
162
+ */
163
+ export const curve25519ScalarMult = (scalar, basepoint = 9n) => {
164
+ let x1 = basepoint % P, x2 = 1n, z2 = 0n, x3 = x1, z3 = 1n, prevbit = 0n;
165
+ // iterate over bits `254`, down to `0`, as per `RFC7748`.
166
+ for (let i = 254n; i >= 0n; i--) {
167
+ const bit = (scalar >> i) & 1n;
168
+ prevbit ^= bit;
169
+ // conditional swap (x2, x3) and (z2, z3) (i.e. the CSwap portion)
170
+ if (prevbit === 1n) {
171
+ [x2, x3, z2, z3] = [x3, x2, z3, z2];
172
+ }
173
+ prevbit = bit;
174
+ // LadderStep computation. note that we need to add `P` in every subtraction step,
175
+ // because javascript's remainder operator is not the same as the mathematical modulo operator,
176
+ // because it preserve negative values that do not have an absolute value above the mod.
177
+ const T1_0 = (x2 + z2) % P, T2_0 = (x2 - z2 + P) % P, T3_0 = (x3 + z3) % P, T4_0 = (x3 - z3 + P) % P, T5_0 = (T1_0 * T1_0) % P, T6_0 = (T2_0 * T2_0) % P,
178
+ // I'm short cutting a few steps, since the wikipedia page uses some variables for temporary swap assignments.
179
+ T2_1 = (T1_0 * T4_0) % P, T1_1 = (T2_1 + T2_0 * T3_0) % P, T5_1 = (T5_0 - T6_0 + P) % P, T1_2 = (a24 * T5_1) % P, T2_2 = (T2_1 * 2n - T1_1 + P) % P,
180
+ // IMPORTANT! I think the wikipedia incorrectly gives the following expression for `T6_1`:
181
+ // `(T6_0 + T1_2) % P`, when it should instead be `(T5_0 + T1_2) % P`
182
+ T6_1 = (T5_0 + T1_2) % P;
183
+ // set the values for the next iteration
184
+ x3 = (T1_1 * T1_1) % P;
185
+ // z3 = (((T2_1 * T2_1) % P) * x1) % P // I don't know how I got this incorrect(?) expression some time ago.
186
+ z3 = (((T2_2 * T2_2) % P) * x1) % P;
187
+ x2 = (T5_0 * T6_0) % P;
188
+ z2 = (T5_1 * T6_1) % P;
189
+ }
190
+ if (prevbit === 1n) {
191
+ [x2, x3, z2, z3] = [x3, x2, z3, z2];
192
+ }
193
+ // compute the `z2` inverse via exponentiation: `z2 ** (p - 2) mod p`
194
+ const z2_inv = modExp(z2, P - 2n, P);
195
+ // result = x2 * z2_inv mod p
196
+ return (x2 * z2_inv) % P;
197
+ };
198
+ /** generate an `x25519` private and public keypair (in 32-byte array form) using the `curve25519` scalar multiplication algorithm.
199
+ *
200
+ * you can optionally provide an existing `private_key`, either as a `bigint`,
201
+ * or as a 32-byte array (`Uint8Array` of size `32`).
202
+ * if your private key is not pre-clamped based on `x25519`'s requirements,
203
+ * it will be forcefully clampped, and the returned {@link KeyPair.privateKey} will be clampped.
204
+ *
205
+ * if you don't provide a private key, this function will randomly generate one for you,
206
+ * using: `crypto.getRandomValues(new Uint8Array(32))`.
207
+ *
208
+ * > [!note]
209
+ * > to convert the key pairs to base64 strings,
210
+ * > use the freshly added (es2025+) `Uint8Array.toBase64` method on each of the keys.
211
+ */
212
+ export const generateX25519Keypair = (private_key) => {
213
+ if (!isBigint(private_key)) {
214
+ // generating random 32-bytes as our private key if it was not already defined.
215
+ private_key = decodeBigintL(private_key ?? crypto.getRandomValues(new Uint8Array(32)));
216
+ }
217
+ private_key = clampX25519Scalar(private_key);
218
+ // the "global common secret" key for generating the public `x25519` key is `9` (now we know why hitler always yelled nein!).
219
+ const public_key = generateX25519SecretKey(private_key, 9n);
220
+ return {
221
+ privateKey: encodeBigintL(private_key, 32),
222
+ publicKey: public_key,
223
+ };
224
+ };
225
+ /** generate a `x25519` secret key that is common between your private key, and your peer's public key.
226
+ *
227
+ * > [!note]
228
+ * > your private key is NOT clampped here! it should already be clampped if it we generated via some `x25519` keygen.
229
+ *
230
+ * @example
231
+ * an "alice and bob" example but with bob replaced with "bobby", so that both names are 5 characters long.
232
+ *
233
+ * ```ts
234
+ * import { assertEquals } from "jsr:@std/assert"
235
+ *
236
+ * // the private keys of both parties (both are incorrectly clampped for `x25519`, but we'll just go with it)
237
+ * const
238
+ * alice_private_key = 123n,
239
+ * bobby_private_key = 789n
240
+ *
241
+ * // both compute a public key, based on a globally agreed upon basekey (global common secret).
242
+ * // this "globally common secret" is `9n` for the `x25519` key-generation.
243
+ * const
244
+ * alice_public_key: Uint8Array = generateX25519SecretKey(alice_private_key, 9n),
245
+ * bobby_public_key: Uint8Array = generateX25519SecretKey(bobby_private_key, 9n)
246
+ *
247
+ * // now, they both exchange their public keys, and then use it as the "basepoint" (a secret between the two),
248
+ * // to _derive_ a _common shared_ secret key, that is the same for both of them.
249
+ * const
250
+ * alice_shared_secret_key: Uint8Array = generateX25519SecretKey(alice_private_key, bobby_public_key),
251
+ * bobby_shared_secret_key: Uint8Array = generateX25519SecretKey(bobby_private_key, alice_public_key)
252
+ *
253
+ * assertEquals(alice_shared_secret_key, bobby_shared_secret_key)
254
+ *
255
+ * // notice that neither alice nor bobby had to exchange this secret key with one another;
256
+ * // they both just computed the same value due to the symmetric nature of `curve25519`.
257
+ * // if a third person, say cluky, were to intercept both public keys,
258
+ * // they won't be able to derive the same secret value that's between alice and bobby, no matter what they try,
259
+ * // unless they manage to get their hands on either one's private key.
260
+ * //
261
+ * // and so now, both alice and bobby can safely use this secret key to encrypt and decrypt each other's messages.
262
+ * // there are many encryption protocols, but a few common ones are: AES256, ChaCha20, HMAC, and the list goes.
263
+ * ```
264
+ */
265
+ export const generateX25519SecretKey = (your_private_key, peer_public_key) => {
266
+ your_private_key = isBigint(your_private_key)
267
+ ? your_private_key
268
+ : decodeBigintL(your_private_key);
269
+ peer_public_key = isBigint(peer_public_key)
270
+ ? peer_public_key
271
+ : decodeBigintL(peer_public_key);
272
+ const secret_key = curve25519ScalarMult(your_private_key, peer_public_key);
273
+ return encodeBigintL(secret_key, 32);
274
+ };
275
+ // TODO: add your AWS Signature V4 Authorization key generator here in the future
276
+ /** SHA-256 hash function.
277
+ *
278
+ * > [!important]
279
+ * > requires `crypto.subtle` to be available in your client's browser, which is only available in `https` sites.
280
+ */
281
+ export const sha256 = async (message) => {
282
+ const msgBuffer = typeof message === "string" ? textEncoder.encode(message) : message, hashBuffer = await crypto.subtle.digest("SHA-256", msgBuffer);
283
+ return hashBuffer;
284
+ };
285
+ /** HMAC-SHA256 function.
286
+ *
287
+ * > [!important]
288
+ * > requires `crypto.subtle` to be available in your client's browser, which is only available in `https` sites.
289
+ */
290
+ export const hmacSha256 = async (encryption_key, message) => {
291
+ const keyBuffer = typeof encryption_key === "string" ? textEncoder.encode(encryption_key) : encryption_key, msgBuffer = typeof message === "string" ? textEncoder.encode(message) : message, cryptoKey = await crypto.subtle.importKey("raw", keyBuffer, { name: "HMAC", hash: { name: "SHA-256" } }, false, ["sign"]), signature = await crypto.subtle.sign("HMAC", cryptoKey, msgBuffer);
292
+ return signature;
293
+ };
294
+ /** apply the {@link hmacSha256} hashing function recursively on multiple messages/binaries.
295
+ *
296
+ * the first message will be used as the encryption key for the second message,
297
+ * and the resulting encrypted message will be used as the encryption key for the third message,
298
+ * and so on, until the final message is encrypted and returned.
299
+ *
300
+ * note that you should provide at least two messages.
301
+ */
302
+ export const hmacSha256Recursive = async (...messages) => {
303
+ let hash = messages.shift();
304
+ while (messages.length > 0) {
305
+ hash = await hmacSha256(hash, messages.shift());
306
+ }
307
+ return hash;
308
+ };
@@ -0,0 +1,10 @@
1
+ /** no external library dependencies. as it should be. */
2
+ /** flags used for minifying (or eliminating) debugging logs and asserts, when an intelligent bundler, such as `esbuild`, is used. */
3
+ export declare const enum DEBUG {
4
+ LOG = 0,
5
+ ASSERT = 0,
6
+ ERROR = 1,
7
+ PRODUCTION = 1,
8
+ MINIFY = 1
9
+ }
10
+ //# sourceMappingURL=deps.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deps.d.ts","sourceRoot":"","sources":["../../../../../../../src/deps/jsr.io/@oazmi/kitchensink/0.10.0/src/deps.ts"],"names":[],"mappings":"AAAA,yDAAyD;AAEzD,qIAAqI;AACrI,0BAAkB,KAAK;IACtB,GAAG,IAAI;IACP,MAAM,IAAI;IACV,KAAK,IAAI;IACT,UAAU,IAAI;IACd,MAAM,IAAI;CACV"}
@@ -0,0 +1,10 @@
1
+ /** no external library dependencies. as it should be. */
2
+ /** flags used for minifying (or eliminating) debugging logs and asserts, when an intelligent bundler, such as `esbuild`, is used. */
3
+ export var DEBUG;
4
+ (function (DEBUG) {
5
+ DEBUG[DEBUG["LOG"] = 0] = "LOG";
6
+ DEBUG[DEBUG["ASSERT"] = 0] = "ASSERT";
7
+ DEBUG[DEBUG["ERROR"] = 1] = "ERROR";
8
+ DEBUG[DEBUG["PRODUCTION"] = 1] = "PRODUCTION";
9
+ DEBUG[DEBUG["MINIFY"] = 1] = "MINIFY";
10
+ })(DEBUG || (DEBUG = {}));