Skip to content

Commit 99a8fe7

Browse files
author
Ivan Mirić
committed
Document ArrayBuffer support in k6/crypto module
Part of #233
1 parent b7cf235 commit 99a8fe7

11 files changed

+138
-90
lines changed

src/data/markdown/docs/02 javascript api/03 k6-crypto/02-createHMAC- algorithm- secret -.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Creates a HMAC hashing object that can then be fed with data repeatedly, and fro
88
| Parameter | Type | Description |
99
| --------- | :----: | :---------------------------------------------------------------------------------------------------------------------------------- |
1010
| algorithm | string | The hashing algorithm to use. One of `md4`, `md5`, `sha1`, `sha256`, `sha384`, `sha512`, `sha512_224`, `sha512_256` or `ripemd160`. |
11-
| secret | string | A shared secret used to sign the data. |
11+
| secret | string / ArrayBuffer | A shared secret used to sign the data. |
1212

1313
### Returns
1414

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
---
22
title: 'hmac( algorithm, secret, data, outputEncoding )'
3-
description: 'Use HMAC to sign an input string.'
3+
description: 'Use HMAC to sign input data.'
44
---
55

66
Use [HMAC](https://en.wikipedia.org/wiki/Hash-based_message_authentication_code) to sign a piece of data using a shared secret.
77

88
| Parameter | Type | Description |
9-
| -------------- | :----: | :---------------------------------------------------------------------------------------------------------------------------------- |
9+
| -------------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------- |
1010
| algorithm | string | The hashing algorithm to use. One of `md4`, `md5`, `sha1`, `sha256`, `sha384`, `sha512`, `sha512_224`, `sha512_256` or `ripemd160`. |
11-
| secret | string | A shared secret used to sign the data. |
12-
| data | string | The data to sign. |
13-
| outputEncoding | string | Describes what type of encoding to use for the hash value. Can be "base64" or "hex". |
11+
| secret | string / ArrayBuffer | A shared secret used to sign the data. |
12+
| data | string / ArrayBuffer | The data to sign. |
13+
| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". |
1414

1515
### Returns
1616

17-
| Type | Description |
18-
| ------ | ------------------------------ |
19-
| string | The string-encoded hash digest |
17+
| Type | Description |
18+
| -------------- | ------------------------------ |
19+
| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). |
20+
2021

2122
### Example
2223

@@ -28,6 +29,9 @@ import crypto from 'k6/crypto';
2829
export default function () {
2930
let hash = crypto.hmac('sha256', 'mysecret', 'hello world!', 'hex');
3031
console.log(hash);
32+
let binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33];
33+
hash = crypto.hmac('sha256', 'mysecret', new Uint8Array(binArray).buffer, 'hex');
34+
console.log(hash);
3135
}
3236
```
3337

@@ -39,6 +43,7 @@ The above script should result in the following being printed during execution:
3943

4044
```bash
4145
INFO[0000] 893a72d8cab129e5ba85aea4599fd53f59bfe652cff4098a3780313228d8c20f
46+
INFO[0000] 893a72d8cab129e5ba85aea4599fd53f59bfe652cff4098a3780313228d8c20f
4247
```
4348

4449
</CodeGroup>
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
---
22
title: 'md4( input, outputEncoding )'
3-
description: 'Use MD4 to hash an input string.'
3+
description: 'Use MD4 to hash input data.'
44
---
55

6-
Use [md4](https://godoc.org/golang.org/x/crypto/md4) to hash an input string.
6+
Use [md4](https://pkg.go.dev/golang.org/x/crypto/md4) to hash input data.
77

8-
| Parameter | Type | Description |
9-
| -------------- | ------ | ------------------------------------------------------------------------------------ |
10-
| input | string | The input string to hash. |
11-
| outputEncoding | string | Describes what type of encoding to use for the hash value. Can be "base64" or "hex". |
8+
| Parameter | Type | Description |
9+
| -------------- | -------------------- | --------------------------------------------------|
10+
| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. |
11+
| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". |
1212

1313
### Returns
1414

15-
| Type | Description |
16-
| ------ | ------------------------------ |
17-
| string | The string-encoded hash digest |
15+
| Type | Description |
16+
| -------------- | ------------------------------ |
17+
| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). |
1818

1919
### Example
2020

@@ -26,6 +26,9 @@ import crypto from 'k6/crypto';
2626
export default function () {
2727
let hash = crypto.md4('hello world!', 'hex');
2828
console.log(hash);
29+
let binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33];
30+
hash = crypto.md4(new Uint8Array(binArray).buffer, 'hex');
31+
console.log(hash);
2932
}
3033
```
3134

@@ -35,4 +38,5 @@ The above script should result in the following being printed during execution:
3538

3639
```bash
3740
INFO[0000] 3363b72840acd5f49f922fef598ee85d
41+
INFO[0000] 3363b72840acd5f49f922fef598ee85d
3842
```
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
---
22
title: 'md5( input, outputEncoding )'
3-
description: 'Use MD5 to hash an input string.'
3+
description: 'Use MD5 to hash input data.'
44
---
55

6-
Use [md5](https://golang.org/pkg/crypto/md5/) to hash an input string.
6+
Use [md5](https://golang.org/pkg/crypto/md5/) to hash input data.
77

8-
| Parameter | Type | Description |
9-
| -------------- | ------ | ------------------------------------------------------------------------------------ |
10-
| input | string | The input string to hash. |
11-
| outputEncoding | string | Describes what type of encoding to use for the hash value. Can be "base64" or "hex". |
8+
| Parameter | Type | Description |
9+
| -------------- | -------------------- | --------------------------------------------------|
10+
| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. |
11+
| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". |
1212

1313
### Returns
1414

15-
| Type | Description |
16-
| ------ | ------------------------------- |
17-
| string | The string-encoded hash digest. |
15+
| Type | Description |
16+
| -------------- | ------------------------------ |
17+
| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). |
1818

1919
### Example
2020

@@ -26,6 +26,9 @@ import crypto from 'k6/crypto';
2626
export default function () {
2727
let hash = crypto.md5('hello world!', 'hex');
2828
console.log(hash);
29+
let binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33];
30+
hash = crypto.md5(new Uint8Array(binArray).buffer, 'hex');
31+
console.log(hash);
2932
}
3033
```
3134

@@ -35,4 +38,5 @@ The above script should result in the following being printed during execution:
3538

3639
```bash
3740
INFO[0000] fc3ff98e8c6a0d3087d515c0473f8677
41+
INFO[0000] fc3ff98e8c6a0d3087d515c0473f8677
3842
```
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
---
22
title: 'ripemd160( input, outputEncoding )'
3-
description: 'Use RIPEMD-160 to hash an input string.'
3+
description: 'Use RIPEMD-160 to hash input data.'
44
---
55

6-
Use [ripemd160](https://godoc.org/golang.org/x/crypto/ripemd160) to hash an input string.
6+
Use [ripemd160](https://pkg.go.dev/golang.org/x/crypto/ripemd160) to hash input data.
77

8-
| Parameter | Type | Description |
9-
| -------------- | ------ | ------------------------------------------------------------------------------------ |
10-
| input | string | The input string to hash. |
11-
| outputEncoding | string | Describes what type of encoding to use for the hash value. Can be "base64" or "hex". |
8+
| Parameter | Type | Description |
9+
| -------------- | -------------------- | --------------------------------------------------|
10+
| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. |
11+
| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". |
1212

1313
### Returns
1414

15-
| Type | Description |
16-
| ------ | ------------------------------- |
17-
| string | The string-encoded hash digest. |
15+
| Type | Description |
16+
| -------------- | ------------------------------ |
17+
| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). |
18+
1819

1920
### Example
2021

@@ -26,6 +27,9 @@ import crypto from 'k6/crypto';
2627
export default function () {
2728
let hash = crypto.ripemd160('hello world!', 'hex');
2829
console.log(hash);
30+
let binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33];
31+
hash = crypto.ripemd160(new Uint8Array(binArray).buffer, 'hex');
32+
console.log(hash);
2933
}
3034
```
3135

@@ -35,4 +39,5 @@ The above script should result in the following being printed during execution:
3539

3640
```bash
3741
INFO[0000] dffd03137b3a333d5754813399a5f437acd694e5
42+
INFO[0000] dffd03137b3a333d5754813399a5f437acd694e5
3843
```
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
---
22
title: 'sha1( input, outputEncoding )'
3-
description: 'Use SHA-1 to hash an input string.'
3+
description: 'Use SHA-1 to hash input data.'
44
---
55

6-
Use [sha1](https://golang.org/pkg/crypto/sha1/) to hash an input string.
6+
Use [sha1](https://golang.org/pkg/crypto/sha1/) to hash input data.
77

8-
| Parameter | Type | Description |
9-
| -------------- | ------ | ------------------------------------------------------------------------------------ |
10-
| input | string | The input string to hash. |
11-
| outputEncoding | string | Describes what type of encoding to use for the hash value. Can be "base64" or "hex". |
8+
| Parameter | Type | Description |
9+
| -------------- | -------------------- | --------------------------------------------------|
10+
| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. |
11+
| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". |
1212

1313
### Returns
1414

15-
| Type | Description |
16-
| ------ | ------------------------------- |
17-
| string | The string-encoded hash digest. |
15+
| Type | Description |
16+
| -------------- | ------------------------------ |
17+
| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). |
18+
1819

1920
### Example
2021

@@ -26,6 +27,9 @@ import crypto from 'k6/crypto';
2627
export default function () {
2728
let hash = crypto.sha1('hello world!', 'hex');
2829
console.log(hash);
30+
let binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33];
31+
hash = crypto.sha1(new Uint8Array(binArray).buffer, 'hex');
32+
console.log(hash);
2933
}
3034
```
3135

@@ -35,4 +39,5 @@ The above script should result in the following being printed during execution:
3539

3640
```bash
3741
INFO[0000] 430ce34d020724ed75a196dfc2ad67c77772d169
42+
INFO[0000] 430ce34d020724ed75a196dfc2ad67c77772d169
3843
```
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
---
22
title: 'sha256( input, outputEncoding )'
3-
description: 'Use SHA-256 to hash an input string.'
3+
description: 'Use SHA-256 to hash input data.'
44
---
55

6-
Use [sha256](https://golang.org/pkg/crypto/sha256/) to hash an input string.
6+
Use [sha256](https://golang.org/pkg/crypto/sha256/) to hash input data.
77

8-
| Parameter | Type | Description |
9-
| -------------- | ------ | ------------------------------------------------------------------------------------ |
10-
| input | string | The input string to hash. |
11-
| outputEncoding | string | Describes what type of encoding to use for the hash value. Can be "base64" or "hex". |
8+
| Parameter | Type | Description |
9+
| -------------- | -------------------- | --------------------------------------------------|
10+
| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. |
11+
| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". |
1212

1313
### Returns
1414

15-
| Type | Description |
16-
| ------ | ------------------------------- |
17-
| string | The string-encoded hash digest. |
15+
| Type | Description |
16+
| -------------- | ------------------------------ |
17+
| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). |
18+
1819

1920
### Example
2021

@@ -26,6 +27,9 @@ import crypto from 'k6/crypto';
2627
export default function () {
2728
let hash = crypto.sha256('hello world!', 'hex');
2829
console.log(hash);
30+
let binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33];
31+
hash = crypto.sha256(new Uint8Array(binArray).buffer, 'hex');
32+
console.log(hash);
2933
}
3034
```
3135

@@ -35,4 +39,5 @@ The above script should result in the following being printed during execution:
3539

3640
```bash
3741
INFO[0000] 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9
42+
INFO[0000] 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9
3843
```
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
---
22
title: 'sha384( input, outputEncoding )'
3-
description: 'Use SHA-384 to hash an input string.'
3+
description: 'Use SHA-384 to hash input data.'
44
---
55

6-
Use sha384 to hash an input string.
6+
Use [sha384](https://golang.org/pkg/crypto/sha512/) to hash input data.
77

8-
| Parameter | Type | Description |
9-
| -------------- | ------ | ------------------------------------------------------------------------------------ |
10-
| input | string | The input string to hash. |
11-
| outputEncoding | string | Describes what type of encoding to use for the hash value. Can be "base64" or "hex". |
8+
| Parameter | Type | Description |
9+
| -------------- | -------------------- | --------------------------------------------------|
10+
| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. |
11+
| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". |
1212

1313
### Returns
1414

15-
| Type | Description |
16-
| ------ | ------------------------------- |
17-
| string | The string-encoded hash digest. |
15+
| Type | Description |
16+
| -------------- | ------------------------------ |
17+
| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). |
18+
1819

1920
### Example
2021

@@ -26,6 +27,9 @@ import crypto from 'k6/crypto';
2627
export default function () {
2728
let hash = crypto.sha384('hello world!', 'hex');
2829
console.log(hash);
30+
let binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33];
31+
hash = crypto.sha384(new Uint8Array(binArray).buffer, 'hex');
32+
console.log(hash);
2933
}
3034
```
3135

@@ -35,4 +39,5 @@ The above script should result in the following being printed during execution:
3539

3640
```bash
3741
INFO[0000] d33d40f7010ce34aa86efd353630309ed5c3d7ffac66d988825cf699f4803ccdf3f033230612f0945332fb580d8af805
42+
INFO[0000] d33d40f7010ce34aa86efd353630309ed5c3d7ffac66d988825cf699f4803ccdf3f033230612f0945332fb580d8af805
3843
```
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
---
22
title: 'sha512( input, outputEncoding )'
3-
description: 'Use SHA-512 to hash an input string.'
3+
description: 'Use SHA-512 to hash input data.'
44
---
55

6-
Use [sha512](https://golang.org/pkg/crypto/sha512/) to hash an input string.
6+
Use [sha512](https://golang.org/pkg/crypto/sha512/) to hash input data.
77

8-
| Parameter | Type | Description |
9-
| -------------- | ------ | ------------------------------------------------------------------------------------ |
10-
| input | string | The input string to hash. |
11-
| outputEncoding | string | Describes what type of encoding to use for the hash value. Can be "base64" or "hex". |
8+
| Parameter | Type | Description |
9+
| -------------- | -------------------- | --------------------------------------------------|
10+
| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. |
11+
| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". |
1212

1313
### Returns
1414

15-
| Type | Description |
16-
| ------ | ------------------------------- |
17-
| string | The string-encoded hash digest. |
15+
| Type | Description |
16+
| -------------- | ------------------------------ |
17+
| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). |
18+
1819

1920
### Example
2021

@@ -26,6 +27,9 @@ import crypto from 'k6/crypto';
2627
export default function () {
2728
let hash = crypto.sha512('hello world!', 'hex');
2829
console.log(hash);
30+
let binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33];
31+
hash = crypto.sha512(new Uint8Array(binArray).buffer, 'hex');
32+
console.log(hash);
2933
}
3034
```
3135

@@ -35,4 +39,5 @@ The above script should result in the following being printed during execution:
3539

3640
```bash
3741
INFO[0000] db9b1cd3262dee37756a09b9064973589847caa8e53d31a9d142ea2701b1b28abd97838bb9a27068ba305dc8d04a45a1fcf079de54d607666996b3cc54f6b67c
42+
INFO[0000] db9b1cd3262dee37756a09b9064973589847caa8e53d31a9d142ea2701b1b28abd97838bb9a27068ba305dc8d04a45a1fcf079de54d607666996b3cc54f6b67c
3843
```

0 commit comments

Comments
 (0)