@cakioe/hmac-signer 1.0.1 → 1.0.3
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 +68 -46
- 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: any, timestamp?: bigint | null): any;
|
|
9
|
+
verify(body: any, timestamp: bigint, signature: string, tolerance?: bigint | null): boolean;
|
|
10
10
|
}
|
package/hmac_signer_bg.js
CHANGED
|
@@ -21,53 +21,30 @@ export class HmacSha256Signer {
|
|
|
21
21
|
return this;
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
|
-
* @param {
|
|
25
|
-
* @param {
|
|
26
|
-
* @returns {
|
|
24
|
+
* @param {any} body
|
|
25
|
+
* @param {bigint | null} [timestamp]
|
|
26
|
+
* @returns {any}
|
|
27
27
|
*/
|
|
28
|
-
sign(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const val2 = BigInt.asIntN(64, timestamp);
|
|
33
|
-
const ptr0 = passArray8ToWasm0(body, wasm.__wbindgen_malloc);
|
|
34
|
-
const len0 = WASM_VECTOR_LEN;
|
|
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 ret = wasm.hmacsha256signer_sign(this.__wbg_ptr, body, !isLikeNone(timestamp), isLikeNone(timestamp) ? BigInt(0) : timestamp);
|
|
30
|
+
if (ret[2]) {
|
|
31
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
47
32
|
}
|
|
33
|
+
return takeFromExternrefTable0(ret[0]);
|
|
48
34
|
}
|
|
49
35
|
/**
|
|
36
|
+
* @param {any} body
|
|
50
37
|
* @param {bigint} timestamp
|
|
51
|
-
* @param {Uint8Array | number[]} body
|
|
52
38
|
* @param {string} signature
|
|
53
39
|
* @param {bigint | null} [tolerance]
|
|
54
40
|
* @returns {boolean}
|
|
55
41
|
*/
|
|
56
|
-
verify(
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
len0 = WASM_VECTOR_LEN;
|
|
63
|
-
ptr1 = passStringToWasm0(signature, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
64
|
-
len1 = WASM_VECTOR_LEN;
|
|
65
|
-
} catch (e) {
|
|
66
|
-
if (ptr1) { wasm.__wbindgen_free(ptr1, len1 * 1, 1); }
|
|
67
|
-
if (ptr0) { wasm.__wbindgen_free(ptr0, len0 * 1, 1); }
|
|
68
|
-
throw e;
|
|
69
|
-
}
|
|
70
|
-
const ret = wasm.hmacsha256signer_verify(this.__wbg_ptr, val3, ptr0, len0, ptr1, len1, !isLikeNone(tolerance), val4);
|
|
42
|
+
verify(body, timestamp, signature, tolerance) {
|
|
43
|
+
const val2 = BigInt.asIntN(64, timestamp);
|
|
44
|
+
const val3 = BigInt.asIntN(64, (isLikeNone(tolerance) ? BigInt(0) : tolerance));
|
|
45
|
+
const ptr0 = passStringToWasm0(signature, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
46
|
+
const len0 = WASM_VECTOR_LEN;
|
|
47
|
+
const ret = wasm.hmacsha256signer_verify(this.__wbg_ptr, body, val2, ptr0, len0, !isLikeNone(tolerance), val3);
|
|
71
48
|
if (ret[2]) {
|
|
72
49
|
throw takeFromExternrefTable0(ret[1]);
|
|
73
50
|
}
|
|
@@ -75,10 +52,39 @@ export class HmacSha256Signer {
|
|
|
75
52
|
}
|
|
76
53
|
}
|
|
77
54
|
if (Symbol.dispose) HmacSha256Signer.prototype[Symbol.dispose] = HmacSha256Signer.prototype.free;
|
|
55
|
+
export function __wbg___wbindgen_string_get_7ce8eee7fd327545(arg0, arg1) {
|
|
56
|
+
const obj = arg1;
|
|
57
|
+
const ret = typeof(obj) === 'string' ? obj : undefined;
|
|
58
|
+
var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
59
|
+
var len1 = WASM_VECTOR_LEN;
|
|
60
|
+
getDataViewMemory0().setInt32((arg0 >>> 0) + 4 * 1, len1, true);
|
|
61
|
+
getDataViewMemory0().setInt32((arg0 >>> 0) + 4 * 0, ptr1, true);
|
|
62
|
+
}
|
|
78
63
|
export function __wbg___wbindgen_throw_4c88d8bf264fd984(arg0, arg1) {
|
|
79
64
|
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
80
65
|
}
|
|
81
|
-
export function
|
|
66
|
+
export function __wbg_new_cd4348290afae586() {
|
|
67
|
+
const ret = new Object();
|
|
68
|
+
return ret;
|
|
69
|
+
}
|
|
70
|
+
export function __wbg_now_463ac581d2fc7ea3() {
|
|
71
|
+
const ret = Date.now();
|
|
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 __wbg_stringify_0a0fbf2ef722380a() { return handleError(function (arg0) {
|
|
79
|
+
const ret = JSON.stringify(arg0);
|
|
80
|
+
return ret;
|
|
81
|
+
}, arguments); }
|
|
82
|
+
export function __wbindgen_generic_0000000000000001(arg0) {
|
|
83
|
+
// Cast intrinsic for `F64 -> Externref`.
|
|
84
|
+
const ret = arg0;
|
|
85
|
+
return ret;
|
|
86
|
+
}
|
|
87
|
+
export function __wbindgen_generic_0000000000000002(arg0, arg1) {
|
|
82
88
|
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
83
89
|
const ret = getStringFromWasm0(arg0, arg1);
|
|
84
90
|
return ret;
|
|
@@ -95,6 +101,20 @@ const HmacSha256SignerFinalization = (typeof FinalizationRegistry === 'undefined
|
|
|
95
101
|
? { register: () => {}, unregister: () => {} }
|
|
96
102
|
: new FinalizationRegistry(ptr => wasm.__wbg_hmacsha256signer_free(ptr, 1));
|
|
97
103
|
|
|
104
|
+
function addToExternrefTable0(obj) {
|
|
105
|
+
const idx = wasm.__externref_table_alloc();
|
|
106
|
+
wasm.__wbindgen_externrefs.set(idx, obj);
|
|
107
|
+
return idx;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let cachedDataViewMemory0 = null;
|
|
111
|
+
function getDataViewMemory0() {
|
|
112
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
|
|
113
|
+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
114
|
+
}
|
|
115
|
+
return cachedDataViewMemory0;
|
|
116
|
+
}
|
|
117
|
+
|
|
98
118
|
function getStringFromWasm0(ptr, len) {
|
|
99
119
|
ptr = ptr >>> 0;
|
|
100
120
|
return decodeText(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
@@ -108,15 +128,17 @@ function getUint8ArrayMemory0() {
|
|
|
108
128
|
return cachedUint8ArrayMemory0;
|
|
109
129
|
}
|
|
110
130
|
|
|
111
|
-
function
|
|
112
|
-
|
|
131
|
+
function handleError(f, args) {
|
|
132
|
+
try {
|
|
133
|
+
return f.apply(this, args);
|
|
134
|
+
} catch (e) {
|
|
135
|
+
const idx = addToExternrefTable0(e);
|
|
136
|
+
wasm.__wbindgen_exn_store(idx);
|
|
137
|
+
}
|
|
113
138
|
}
|
|
114
139
|
|
|
115
|
-
function
|
|
116
|
-
|
|
117
|
-
getUint8ArrayMemory0().set(arg, ptr / 1);
|
|
118
|
-
WASM_VECTOR_LEN = arg.length;
|
|
119
|
-
return ptr;
|
|
140
|
+
function isLikeNone(x) {
|
|
141
|
+
return x === undefined || x === null;
|
|
120
142
|
}
|
|
121
143
|
|
|
122
144
|
function passStringToWasm0(arg, malloc, realloc) {
|
package/hmac_signer_bg.wasm
CHANGED
|
Binary file
|