@cakioe/hmac-signer 0.0.0-stage → 1.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 cleveng
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,279 @@
1
- # Temporary Holding Version
1
+ # HMAC SHA256 Signer
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A lightweight HMAC-SHA256 signing and verification library written in Rust.
4
+
5
+ Supports both native Rust applications and WebAssembly (WASM), allowing the same implementation to be used in Rust, JavaScript, and TypeScript projects.
6
+
7
+ ## Features
8
+
9
+ - HMAC-SHA256 signature generation.
10
+ - HMAC-SHA256 signature verification.
11
+ - Optional timestamp tolerance validation.
12
+ - Hexadecimal signature encoding.
13
+ - Constant-time signature verification.
14
+ - Native Rust library support.
15
+ - WebAssembly support.
16
+ - TypeScript type definitions through `wasm-bindgen`.
17
+
18
+ ## Installation
19
+
20
+ ### Rust
21
+
22
+ Add the dependency to your `Cargo.toml`:
23
+
24
+ ```toml
25
+ [dependencies]
26
+ hmac-signer = "1.0.0"
27
+ ```
28
+
29
+ ### TypeScript / JavaScript
30
+
31
+ Install from npm:
32
+
33
+ ```bash
34
+ pnpm add hmac-signer
35
+ ```
36
+
37
+ Or:
38
+
39
+ ```bash
40
+ npm install hmac-signer
41
+ ```
42
+
43
+ ## Usage
44
+
45
+ ### Rust usage
46
+
47
+ Import the signer:
48
+
49
+ ```rust
50
+ use hmac_signer::{HmacSha256Signer, HmacSha256SignerTrait};
51
+ ```
52
+
53
+ #### Create a signer
54
+
55
+ ```rust
56
+ let signer = HmacSha256Signer::new("your-secret-key");
57
+ ```
58
+
59
+ #### Generate a signature
60
+
61
+ ```rust
62
+ let timestamp = 1793352268_i64;
63
+ let body = br#"{"amount":100,"currency":"USD"}"#;
64
+
65
+ let signature = signer.sign(timestamp, body)?;
66
+
67
+ println!("Signature: {}", signature);
68
+ ```
69
+
70
+ #### Verify a signature
71
+
72
+ ```rust
73
+ let valid = signer.verify(
74
+ timestamp,
75
+ body,
76
+ &signature,
77
+ Some(300),
78
+ )?;
79
+
80
+ println!("Valid: {}", valid);
81
+ ```
82
+
83
+ #### Disable timestamp validation
84
+
85
+ Pass `None` as the tolerance parameter:
86
+
87
+ ```rust
88
+ let valid = signer.verify(
89
+ timestamp,
90
+ body,
91
+ &signature,
92
+ None,
93
+ )?;
94
+ ```
95
+
96
+ ### TypeScript / JavaScript usage
97
+
98
+ Import the signer:
99
+
100
+ ```typescript
101
+ import { HmacSha256Signer } from 'hmac-signer';
102
+ ```
103
+
104
+ #### Create a signer by typescript
105
+
106
+ ```typescript
107
+ const signer = new HmacSha256Signer('your-secret-key');
108
+ ```
109
+
110
+ #### Generate a signature by typescript
111
+
112
+ ```typescript
113
+ const timestamp = BigInt(Math.floor(Date.now() / 1000));
114
+
115
+ const body = new TextEncoder().encode(
116
+ JSON.stringify({
117
+ amount: 100,
118
+ currency: 'USD',
119
+ })
120
+ );
121
+
122
+ const signature = signer.sign(timestamp, body);
123
+
124
+ console.log(signature);
125
+ ```
126
+
127
+ #### Verify a signature by typescript
128
+
129
+ ```typescript
130
+ const valid = signer.verify(timestamp, body, signature, 300n);
131
+
132
+ console.log(valid);
133
+ ```
134
+
135
+ #### Disable timestamp validation by typescript
136
+
137
+ Pass `undefined`:
138
+
139
+ ```typescript
140
+ const valid = signer.verify(timestamp, body, signature, undefined);
141
+ ```
142
+
143
+ ## Signature Format
144
+
145
+ The signature is generated using HMAC-SHA256.
146
+
147
+ The signed message format is:
148
+
149
+ ```text
150
+ timestamp + "." + request_body
151
+ ```
152
+
153
+ The signature generation algorithm:
154
+
155
+ ```text
156
+ HMAC-SHA256(
157
+ secret,
158
+ timestamp + "." + request_body
159
+ )
160
+ ```
161
+
162
+ The resulting digest is encoded as a lowercase hexadecimal string.
163
+
164
+ Example:
165
+
166
+ ```text
167
+ timestamp = 1793352268
168
+ body = {"amount":100}
169
+
170
+ message = 1793352268.{"amount":100}
171
+
172
+ signature = HMAC-SHA256(secret, message)
173
+ ```
174
+
175
+ ## Timestamp Tolerance
176
+
177
+ The `verify` method accepts an optional `tolerance` parameter.
178
+
179
+ | Parameter | Description |
180
+ | ----------- | ------------------------------------------------- |
181
+ | `Some(300)` | Allow a timestamp difference of up to 300 seconds |
182
+ | `Some(60)` | Allow a timestamp difference of up to 60 seconds |
183
+ | `Some(0)` | Require the timestamp to match the current second |
184
+ | `None` | Disable timestamp validation |
185
+
186
+ If the timestamp is outside the allowed window, verification returns an error.
187
+
188
+ ## API Reference
189
+
190
+ ### HmacSha256Signer
191
+
192
+ Creates an HMAC-SHA256 signer.
193
+
194
+ ```rust
195
+ HmacSha256Signer::new(secret: impl Into<String>) -> Self
196
+ ```
197
+
198
+ ### sign
199
+
200
+ Generates a signature.
201
+
202
+ ```rust
203
+ fn sign(
204
+ &self,
205
+ timestamp: i64,
206
+ body: &[u8],
207
+ ) -> Result<String, anyhow::Error>
208
+ ```
209
+
210
+ ### verify
211
+
212
+ Verifies a signature.
213
+
214
+ ```rust
215
+ fn verify(
216
+ &self,
217
+ timestamp: i64,
218
+ body: &[u8],
219
+ signature: &str,
220
+ tolerance: Option<i64>,
221
+ ) -> Result<bool, anyhow::Error>
222
+ ```
223
+
224
+ ## Building from Source
225
+
226
+ ### Native Rust
227
+
228
+ ```bash
229
+ cargo build --release
230
+ ```
231
+
232
+ Run tests:
233
+
234
+ ```bash
235
+ cargo test
236
+ ```
237
+
238
+ ### WebAssembly
239
+
240
+ Install the WASM target:
241
+
242
+ ```bash
243
+ rustup target add wasm32-unknown-unknown
244
+ ```
245
+
246
+ Install `wasm-pack`:
247
+
248
+ ```bash
249
+ cargo install wasm-pack
250
+ ```
251
+
252
+ Build the WASM package:
253
+
254
+ ```bash
255
+ wasm-pack build --target bundler --release -- --features wasm
256
+
257
+ wasm-pack build \
258
+ --target bundler \
259
+ --release \
260
+ --scope cakioe \
261
+ --out-dir pkg \
262
+ --out-name hmac_signer \
263
+ -- --features wasm
264
+ ```
265
+
266
+ The generated npm package will be available in the `pkg/` directory.
267
+
268
+ ## Security Considerations
269
+
270
+ - Keep your secret key confidential.
271
+ - Always use HTTPS when transmitting signatures.
272
+ - Use timestamp validation to reduce replay attack risks.
273
+ - Verify the original raw request body instead of re-serialized JSON.
274
+ - Use sufficiently random secret keys.
275
+ - Implement replay protection when strict request uniqueness is required.
276
+
277
+ ## License
278
+
279
+ MIT
@@ -0,0 +1,8 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export class HmacSha256Signer {
5
+ private constructor();
6
+ free(): void;
7
+ [Symbol.dispose](): void;
8
+ }
package/hmac_signer.js ADDED
@@ -0,0 +1,9 @@
1
+ /* @ts-self-types="./hmac_signer.d.ts" */
2
+ import * as wasm from "./hmac_signer_bg.wasm";
3
+ import { __wbg_set_wasm } from "./hmac_signer_bg.js";
4
+
5
+ __wbg_set_wasm(wasm);
6
+ wasm.__wbindgen_start();
7
+ export {
8
+ HmacSha256Signer
9
+ } from "./hmac_signer_bg.js";
@@ -0,0 +1,60 @@
1
+ export class HmacSha256Signer {
2
+ __destroy_into_raw() {
3
+ const ptr = this.__wbg_ptr;
4
+ this.__wbg_ptr = 0;
5
+ HmacSha256SignerFinalization.unregister(this);
6
+ return ptr;
7
+ }
8
+ free() {
9
+ const ptr = this.__destroy_into_raw();
10
+ wasm.__wbg_hmacsha256signer_free(ptr, 0);
11
+ }
12
+ }
13
+ if (Symbol.dispose) HmacSha256Signer.prototype[Symbol.dispose] = HmacSha256Signer.prototype.free;
14
+ export function __wbg___wbindgen_throw_4c88d8bf264fd984(arg0, arg1) {
15
+ throw new Error(getStringFromWasm0(arg0, arg1));
16
+ }
17
+ export function __wbindgen_init_externref_table() {
18
+ const table = wasm.__wbindgen_externrefs;
19
+ table.set(0, undefined);
20
+ table.set(1024, undefined);
21
+ table.set(1025, null);
22
+ table.set(1026, true);
23
+ table.set(1027, false);
24
+ }
25
+ const HmacSha256SignerFinalization = (typeof FinalizationRegistry === 'undefined')
26
+ ? { register: () => {}, unregister: () => {} }
27
+ : new FinalizationRegistry(ptr => wasm.__wbg_hmacsha256signer_free(ptr, 1));
28
+
29
+ function getStringFromWasm0(ptr, len) {
30
+ ptr = ptr >>> 0;
31
+ return decodeText(getUint8ArrayMemory0().subarray(ptr, ptr + len));
32
+ }
33
+
34
+ let cachedUint8ArrayMemory0 = null;
35
+ function getUint8ArrayMemory0() {
36
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
37
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
38
+ }
39
+ return cachedUint8ArrayMemory0;
40
+ }
41
+
42
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true });
43
+ cachedTextDecoder.decode();
44
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
45
+ let numBytesDecoded = 0;
46
+ function decodeText(bytes) {
47
+ numBytesDecoded += bytes.length;
48
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
49
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true });
50
+ cachedTextDecoder.decode();
51
+ numBytesDecoded = bytes.length;
52
+ }
53
+ return cachedTextDecoder.decode(bytes);
54
+ }
55
+
56
+
57
+ let wasm;
58
+ export function __wbg_set_wasm(val) {
59
+ wasm = val;
60
+ }
Binary file
package/package.json CHANGED
@@ -1,6 +1,26 @@
1
1
  {
2
2
  "name": "@cakioe/hmac-signer",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "type": "module",
4
+ "collaborators": [
5
+ "cleveng <cleveng@gmail.com>"
6
+ ],
7
+ "description": "Hmac Signer By Rust",
8
+ "version": "1.0.0",
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/cleveng/hmac-signer.git"
13
+ },
14
+ "files": [
15
+ "hmac_signer_bg.wasm",
16
+ "hmac_signer.js",
17
+ "hmac_signer_bg.js",
18
+ "hmac_signer.d.ts"
19
+ ],
20
+ "main": "hmac_signer.js",
21
+ "types": "hmac_signer.d.ts",
22
+ "sideEffects": [
23
+ "./hmac_signer.js",
24
+ "./snippets/*"
25
+ ]
26
+ }