@cakioe/hmac-signer 1.0.0 → 1.0.2
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/README.md +13 -10
- package/hmac_signer.d.ts +3 -1
- package/hmac_signer_bg.js +155 -0
- package/hmac_signer_bg.wasm +0 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -62,7 +62,7 @@ let signer = HmacSha256Signer::new("your-secret-key");
|
|
|
62
62
|
let timestamp = 1793352268_i64;
|
|
63
63
|
let body = br#"{"amount":100,"currency":"USD"}"#;
|
|
64
64
|
|
|
65
|
-
let signature = signer.sign(
|
|
65
|
+
let signature = signer.sign(body, timestamp)?;
|
|
66
66
|
|
|
67
67
|
println!("Signature: {}", signature);
|
|
68
68
|
```
|
|
@@ -71,8 +71,8 @@ println!("Signature: {}", signature);
|
|
|
71
71
|
|
|
72
72
|
```rust
|
|
73
73
|
let valid = signer.verify(
|
|
74
|
-
timestamp,
|
|
75
74
|
body,
|
|
75
|
+
timestamp,
|
|
76
76
|
&signature,
|
|
77
77
|
Some(300),
|
|
78
78
|
)?;
|
|
@@ -86,8 +86,8 @@ Pass `None` as the tolerance parameter:
|
|
|
86
86
|
|
|
87
87
|
```rust
|
|
88
88
|
let valid = signer.verify(
|
|
89
|
-
timestamp,
|
|
90
89
|
body,
|
|
90
|
+
timestamp,
|
|
91
91
|
&signature,
|
|
92
92
|
None,
|
|
93
93
|
)?;
|
|
@@ -119,7 +119,7 @@ const body = new TextEncoder().encode(
|
|
|
119
119
|
})
|
|
120
120
|
);
|
|
121
121
|
|
|
122
|
-
const signature = signer.sign(
|
|
122
|
+
const signature = signer.sign(body, timestamp);
|
|
123
123
|
|
|
124
124
|
console.log(signature);
|
|
125
125
|
```
|
|
@@ -127,7 +127,7 @@ console.log(signature);
|
|
|
127
127
|
#### Verify a signature by typescript
|
|
128
128
|
|
|
129
129
|
```typescript
|
|
130
|
-
const valid = signer.verify(
|
|
130
|
+
const valid = signer.verify(body, timestamp, signature, 300n);
|
|
131
131
|
|
|
132
132
|
console.log(valid);
|
|
133
133
|
```
|
|
@@ -137,7 +137,7 @@ console.log(valid);
|
|
|
137
137
|
Pass `undefined`:
|
|
138
138
|
|
|
139
139
|
```typescript
|
|
140
|
-
const valid = signer.verify(
|
|
140
|
+
const valid = signer.verify(body, timestamp, signature, undefined);
|
|
141
141
|
```
|
|
142
142
|
|
|
143
143
|
## Signature Format
|
|
@@ -202,9 +202,9 @@ Generates a signature.
|
|
|
202
202
|
```rust
|
|
203
203
|
fn sign(
|
|
204
204
|
&self,
|
|
205
|
-
timestamp: i64,
|
|
206
205
|
body: &[u8],
|
|
207
|
-
|
|
206
|
+
timestamp: Option<i64>,
|
|
207
|
+
) -> Result<(i64, String), anyhow::Error>
|
|
208
208
|
```
|
|
209
209
|
|
|
210
210
|
### verify
|
|
@@ -214,8 +214,8 @@ Verifies a signature.
|
|
|
214
214
|
```rust
|
|
215
215
|
fn verify(
|
|
216
216
|
&self,
|
|
217
|
-
timestamp: i64,
|
|
218
217
|
body: &[u8],
|
|
218
|
+
timestamp: i64,
|
|
219
219
|
signature: &str,
|
|
220
220
|
tolerance: Option<i64>,
|
|
221
221
|
) -> Result<bool, anyhow::Error>
|
|
@@ -226,7 +226,7 @@ fn verify(
|
|
|
226
226
|
### Native Rust
|
|
227
227
|
|
|
228
228
|
```bash
|
|
229
|
-
cargo build --release
|
|
229
|
+
cargo build --release -vvv
|
|
230
230
|
```
|
|
231
231
|
|
|
232
232
|
Run tests:
|
|
@@ -261,6 +261,9 @@ wasm-pack build \
|
|
|
261
261
|
--out-dir pkg \
|
|
262
262
|
--out-name hmac_signer \
|
|
263
263
|
-- --features wasm
|
|
264
|
+
|
|
265
|
+
cd pkg
|
|
266
|
+
npm stage publish --access public
|
|
264
267
|
```
|
|
265
268
|
|
|
266
269
|
The generated npm package will be available in the `pkg/` directory.
|
package/hmac_signer.d.ts
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
/* eslint-disable */
|
|
3
3
|
|
|
4
4
|
export class HmacSha256Signer {
|
|
5
|
-
private constructor();
|
|
6
5
|
free(): void;
|
|
7
6
|
[Symbol.dispose](): void;
|
|
7
|
+
constructor(secret: string);
|
|
8
|
+
sign(body: Uint8Array | number[], timestamp?: bigint | null): any;
|
|
9
|
+
verify(body: Uint8Array | number[], timestamp: bigint, signature: string, tolerance?: bigint | null): boolean;
|
|
8
10
|
}
|
package/hmac_signer_bg.js
CHANGED
|
@@ -9,11 +9,82 @@ export class HmacSha256Signer {
|
|
|
9
9
|
const ptr = this.__destroy_into_raw();
|
|
10
10
|
wasm.__wbg_hmacsha256signer_free(ptr, 0);
|
|
11
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} secret
|
|
14
|
+
*/
|
|
15
|
+
constructor(secret) {
|
|
16
|
+
const ptr0 = passStringToWasm0(secret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
17
|
+
const len0 = WASM_VECTOR_LEN;
|
|
18
|
+
const ret = wasm.hmacsha256signer_new(ptr0, len0);
|
|
19
|
+
this.__wbg_ptr = ret;
|
|
20
|
+
HmacSha256SignerFinalization.register(this, this.__wbg_ptr, this);
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @param {Uint8Array | number[]} body
|
|
25
|
+
* @param {bigint | null} [timestamp]
|
|
26
|
+
* @returns {any}
|
|
27
|
+
*/
|
|
28
|
+
sign(body, timestamp) {
|
|
29
|
+
const val2 = BigInt.asIntN(64, (isLikeNone(timestamp) ? BigInt(0) : timestamp));
|
|
30
|
+
const ptr0 = passArray8ToWasm0(body, wasm.__wbindgen_malloc);
|
|
31
|
+
const len0 = WASM_VECTOR_LEN;
|
|
32
|
+
const ret = wasm.hmacsha256signer_sign(this.__wbg_ptr, ptr0, len0, !isLikeNone(timestamp), val2);
|
|
33
|
+
if (ret[2]) {
|
|
34
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
35
|
+
}
|
|
36
|
+
return takeFromExternrefTable0(ret[0]);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* @param {Uint8Array | number[]} body
|
|
40
|
+
* @param {bigint} timestamp
|
|
41
|
+
* @param {string} signature
|
|
42
|
+
* @param {bigint | null} [tolerance]
|
|
43
|
+
* @returns {boolean}
|
|
44
|
+
*/
|
|
45
|
+
verify(body, timestamp, signature, tolerance) {
|
|
46
|
+
const val3 = BigInt.asIntN(64, timestamp);
|
|
47
|
+
const val4 = BigInt.asIntN(64, (isLikeNone(tolerance) ? BigInt(0) : tolerance));
|
|
48
|
+
let ptr0, len0, ptr1, len1;
|
|
49
|
+
try {
|
|
50
|
+
ptr0 = passArray8ToWasm0(body, wasm.__wbindgen_malloc);
|
|
51
|
+
len0 = WASM_VECTOR_LEN;
|
|
52
|
+
ptr1 = passStringToWasm0(signature, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
53
|
+
len1 = WASM_VECTOR_LEN;
|
|
54
|
+
} catch (e) {
|
|
55
|
+
if (ptr1) { wasm.__wbindgen_free(ptr1, len1 * 1, 1); }
|
|
56
|
+
if (ptr0) { wasm.__wbindgen_free(ptr0, len0 * 1, 1); }
|
|
57
|
+
throw e;
|
|
58
|
+
}
|
|
59
|
+
const ret = wasm.hmacsha256signer_verify(this.__wbg_ptr, ptr0, len0, val3, ptr1, len1, !isLikeNone(tolerance), val4);
|
|
60
|
+
if (ret[2]) {
|
|
61
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
62
|
+
}
|
|
63
|
+
return ret[0] !== 0;
|
|
64
|
+
}
|
|
12
65
|
}
|
|
13
66
|
if (Symbol.dispose) HmacSha256Signer.prototype[Symbol.dispose] = HmacSha256Signer.prototype.free;
|
|
14
67
|
export function __wbg___wbindgen_throw_4c88d8bf264fd984(arg0, arg1) {
|
|
15
68
|
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
16
69
|
}
|
|
70
|
+
export function __wbg_new_cd4348290afae586() {
|
|
71
|
+
const ret = new Object();
|
|
72
|
+
return ret;
|
|
73
|
+
}
|
|
74
|
+
export function __wbg_set_2793479888329cb6() { return handleError(function (arg0, arg1, arg2) {
|
|
75
|
+
const ret = Reflect.set(arg0, arg1, arg2);
|
|
76
|
+
return ret;
|
|
77
|
+
}, arguments); }
|
|
78
|
+
export function __wbindgen_generic_0000000000000001(arg0) {
|
|
79
|
+
// Cast intrinsic for `F64 -> Externref`.
|
|
80
|
+
const ret = arg0;
|
|
81
|
+
return ret;
|
|
82
|
+
}
|
|
83
|
+
export function __wbindgen_generic_0000000000000002(arg0, arg1) {
|
|
84
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
85
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
86
|
+
return ret;
|
|
87
|
+
}
|
|
17
88
|
export function __wbindgen_init_externref_table() {
|
|
18
89
|
const table = wasm.__wbindgen_externrefs;
|
|
19
90
|
table.set(0, undefined);
|
|
@@ -26,6 +97,12 @@ const HmacSha256SignerFinalization = (typeof FinalizationRegistry === 'undefined
|
|
|
26
97
|
? { register: () => {}, unregister: () => {} }
|
|
27
98
|
: new FinalizationRegistry(ptr => wasm.__wbg_hmacsha256signer_free(ptr, 1));
|
|
28
99
|
|
|
100
|
+
function addToExternrefTable0(obj) {
|
|
101
|
+
const idx = wasm.__externref_table_alloc();
|
|
102
|
+
wasm.__wbindgen_externrefs.set(idx, obj);
|
|
103
|
+
return idx;
|
|
104
|
+
}
|
|
105
|
+
|
|
29
106
|
function getStringFromWasm0(ptr, len) {
|
|
30
107
|
ptr = ptr >>> 0;
|
|
31
108
|
return decodeText(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
@@ -39,6 +116,69 @@ function getUint8ArrayMemory0() {
|
|
|
39
116
|
return cachedUint8ArrayMemory0;
|
|
40
117
|
}
|
|
41
118
|
|
|
119
|
+
function handleError(f, args) {
|
|
120
|
+
try {
|
|
121
|
+
return f.apply(this, args);
|
|
122
|
+
} catch (e) {
|
|
123
|
+
const idx = addToExternrefTable0(e);
|
|
124
|
+
wasm.__wbindgen_exn_store(idx);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function isLikeNone(x) {
|
|
129
|
+
return x === undefined || x === null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function passArray8ToWasm0(arg, malloc) {
|
|
133
|
+
const ptr = malloc(arg.length * 1, 1) >>> 0;
|
|
134
|
+
getUint8ArrayMemory0().set(arg, ptr / 1);
|
|
135
|
+
WASM_VECTOR_LEN = arg.length;
|
|
136
|
+
return ptr;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
140
|
+
if (realloc === undefined) {
|
|
141
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
142
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
143
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
144
|
+
WASM_VECTOR_LEN = buf.length;
|
|
145
|
+
return ptr;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let len = arg.length;
|
|
149
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
150
|
+
|
|
151
|
+
const mem = getUint8ArrayMemory0();
|
|
152
|
+
|
|
153
|
+
let offset = 0;
|
|
154
|
+
|
|
155
|
+
for (; offset < len; offset++) {
|
|
156
|
+
const code = arg.charCodeAt(offset);
|
|
157
|
+
if (code > 0x7F) break;
|
|
158
|
+
mem[ptr + offset] = code;
|
|
159
|
+
}
|
|
160
|
+
if (offset !== len) {
|
|
161
|
+
if (offset !== 0) {
|
|
162
|
+
arg = arg.slice(offset);
|
|
163
|
+
}
|
|
164
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
165
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
166
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
167
|
+
|
|
168
|
+
offset += ret.written;
|
|
169
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
WASM_VECTOR_LEN = offset;
|
|
173
|
+
return ptr;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function takeFromExternrefTable0(idx) {
|
|
177
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
178
|
+
wasm.__externref_table_dealloc(idx);
|
|
179
|
+
return value;
|
|
180
|
+
}
|
|
181
|
+
|
|
42
182
|
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true });
|
|
43
183
|
cachedTextDecoder.decode();
|
|
44
184
|
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
@@ -53,6 +193,21 @@ function decodeText(bytes) {
|
|
|
53
193
|
return cachedTextDecoder.decode(bytes);
|
|
54
194
|
}
|
|
55
195
|
|
|
196
|
+
const cachedTextEncoder = new TextEncoder();
|
|
197
|
+
|
|
198
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
199
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
200
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
201
|
+
view.set(buf);
|
|
202
|
+
return {
|
|
203
|
+
read: arg.length,
|
|
204
|
+
written: buf.length
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
let WASM_VECTOR_LEN = 0;
|
|
210
|
+
|
|
56
211
|
|
|
57
212
|
let wasm;
|
|
58
213
|
export function __wbg_set_wasm(val) {
|
package/hmac_signer_bg.wasm
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
"cleveng <cleveng@gmail.com>"
|
|
6
6
|
],
|
|
7
7
|
"description": "Hmac Signer By Rust",
|
|
8
|
-
"version": "1.0.
|
|
8
|
+
"version": "1.0.2",
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
|
-
"url": "
|
|
12
|
+
"url": "https://github.com/cleveng/hmac-signer.git"
|
|
13
13
|
},
|
|
14
14
|
"files": [
|
|
15
15
|
"hmac_signer_bg.wasm",
|