@cakioe/hmac-signer 1.0.1 → 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 +10 -10
- package/hmac_signer.d.ts +2 -2
- package/hmac_signer_bg.js +42 -25
- package/hmac_signer_bg.wasm +0 -0
- package/package.json +1 -1
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:
|
package/hmac_signer.d.ts
CHANGED
|
@@ -5,6 +5,6 @@ export class HmacSha256Signer {
|
|
|
5
5
|
free(): void;
|
|
6
6
|
[Symbol.dispose](): void;
|
|
7
7
|
constructor(secret: string);
|
|
8
|
-
sign(
|
|
9
|
-
verify(
|
|
8
|
+
sign(body: Uint8Array | number[], timestamp?: bigint | null): any;
|
|
9
|
+
verify(body: Uint8Array | number[], timestamp: bigint, signature: string, tolerance?: bigint | null): boolean;
|
|
10
10
|
}
|
package/hmac_signer_bg.js
CHANGED
|
@@ -21,39 +21,28 @@ export class HmacSha256Signer {
|
|
|
21
21
|
return this;
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
|
-
* @param {bigint} timestamp
|
|
25
24
|
* @param {Uint8Array | number[]} body
|
|
26
|
-
* @
|
|
25
|
+
* @param {bigint | null} [timestamp]
|
|
26
|
+
* @returns {any}
|
|
27
27
|
*/
|
|
28
|
-
sign(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const ret = wasm.hmacsha256signer_sign(this.__wbg_ptr, val2, ptr0, len0);
|
|
36
|
-
var ptr3 = ret[0];
|
|
37
|
-
var len3 = ret[1];
|
|
38
|
-
if (ret[3]) {
|
|
39
|
-
ptr3 = 0; len3 = 0;
|
|
40
|
-
throw takeFromExternrefTable0(ret[2]);
|
|
41
|
-
}
|
|
42
|
-
deferred4_0 = ptr3;
|
|
43
|
-
deferred4_1 = len3;
|
|
44
|
-
return getStringFromWasm0(ptr3, len3);
|
|
45
|
-
} finally {
|
|
46
|
-
wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
|
|
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]);
|
|
47
35
|
}
|
|
36
|
+
return takeFromExternrefTable0(ret[0]);
|
|
48
37
|
}
|
|
49
38
|
/**
|
|
50
|
-
* @param {bigint} timestamp
|
|
51
39
|
* @param {Uint8Array | number[]} body
|
|
40
|
+
* @param {bigint} timestamp
|
|
52
41
|
* @param {string} signature
|
|
53
42
|
* @param {bigint | null} [tolerance]
|
|
54
43
|
* @returns {boolean}
|
|
55
44
|
*/
|
|
56
|
-
verify(
|
|
45
|
+
verify(body, timestamp, signature, tolerance) {
|
|
57
46
|
const val3 = BigInt.asIntN(64, timestamp);
|
|
58
47
|
const val4 = BigInt.asIntN(64, (isLikeNone(tolerance) ? BigInt(0) : tolerance));
|
|
59
48
|
let ptr0, len0, ptr1, len1;
|
|
@@ -67,7 +56,7 @@ export class HmacSha256Signer {
|
|
|
67
56
|
if (ptr0) { wasm.__wbindgen_free(ptr0, len0 * 1, 1); }
|
|
68
57
|
throw e;
|
|
69
58
|
}
|
|
70
|
-
const ret = wasm.hmacsha256signer_verify(this.__wbg_ptr,
|
|
59
|
+
const ret = wasm.hmacsha256signer_verify(this.__wbg_ptr, ptr0, len0, val3, ptr1, len1, !isLikeNone(tolerance), val4);
|
|
71
60
|
if (ret[2]) {
|
|
72
61
|
throw takeFromExternrefTable0(ret[1]);
|
|
73
62
|
}
|
|
@@ -78,7 +67,20 @@ if (Symbol.dispose) HmacSha256Signer.prototype[Symbol.dispose] = HmacSha256Signe
|
|
|
78
67
|
export function __wbg___wbindgen_throw_4c88d8bf264fd984(arg0, arg1) {
|
|
79
68
|
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
80
69
|
}
|
|
81
|
-
export function
|
|
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) {
|
|
82
84
|
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
83
85
|
const ret = getStringFromWasm0(arg0, arg1);
|
|
84
86
|
return ret;
|
|
@@ -95,6 +97,12 @@ const HmacSha256SignerFinalization = (typeof FinalizationRegistry === 'undefined
|
|
|
95
97
|
? { register: () => {}, unregister: () => {} }
|
|
96
98
|
: new FinalizationRegistry(ptr => wasm.__wbg_hmacsha256signer_free(ptr, 1));
|
|
97
99
|
|
|
100
|
+
function addToExternrefTable0(obj) {
|
|
101
|
+
const idx = wasm.__externref_table_alloc();
|
|
102
|
+
wasm.__wbindgen_externrefs.set(idx, obj);
|
|
103
|
+
return idx;
|
|
104
|
+
}
|
|
105
|
+
|
|
98
106
|
function getStringFromWasm0(ptr, len) {
|
|
99
107
|
ptr = ptr >>> 0;
|
|
100
108
|
return decodeText(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
@@ -108,6 +116,15 @@ function getUint8ArrayMemory0() {
|
|
|
108
116
|
return cachedUint8ArrayMemory0;
|
|
109
117
|
}
|
|
110
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
|
+
|
|
111
128
|
function isLikeNone(x) {
|
|
112
129
|
return x === undefined || x === null;
|
|
113
130
|
}
|
package/hmac_signer_bg.wasm
CHANGED
|
Binary file
|