1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 18:19:08 -05:00
denoland-deno/std/hash/README.md

84 lines
1.8 KiB
Markdown
Raw Normal View History

2020-06-06 12:36:34 -04:00
# std/hash
## Usage
2020-06-06 12:36:34 -04:00
### Creating new hash instance
You can create a new Hasher instance by calling `createHash` defined in mod.ts.
2020-06-06 12:36:34 -04:00
```ts
import { createHash } from "https://deno.land/std/hash/mod.ts";
2020-06-06 12:36:34 -04:00
const hash = createHash("md5");
// ...
2020-06-06 12:36:34 -04:00
```
### Using hash instance
2020-06-06 12:36:34 -04:00
You can use `update` method to feed data into your hash instance. Call `digest`
method to retrive final hash value in ArrayBuffer.
2020-06-06 12:36:34 -04:00
```ts
import { createHash } from "https://deno.land/std/hash/mod.ts";
2020-06-06 12:36:34 -04:00
const hash = createHash("md5");
hash.update("Your data here");
const final = hash.digest(); // returns ArrayBuffer
```
2020-06-06 12:36:34 -04:00
Please note that `digest` invalidates the hash instance's internal state.
Calling `digest` more than once will throw an Error.
2020-06-06 12:36:34 -04:00
```ts
import { createHash } from "https://deno.land/std/hash/mod.ts";
2020-06-06 12:36:34 -04:00
const hash = createHash("md5");
hash.update("Your data here");
const final1 = hash.digest(); // returns ArrayBuffer
const final2 = hash.digest(); // throws Error
2020-06-06 12:36:34 -04:00
```
If you need final hash in string formats, call `toString` method with output
format.
2020-06-06 12:36:34 -04:00
Supported formats are `hex` and `base64` and default format is `hex`.
2020-06-06 12:36:34 -04:00
```ts
import { createHash } from "https://deno.land/std/hash/mod.ts";
2020-06-06 12:36:34 -04:00
const hash = createHash("md5");
hash.update("Your data here");
const hashInHex = hash.toString(); // returns 5fe084ee423ff7e0c7709e9437cee89d
```
2020-06-06 12:36:34 -04:00
```ts
import { createHash } from "https://deno.land/std/hash/mod.ts";
2020-06-06 12:36:34 -04:00
const hash = createHash("md5");
hash.update("Your data here");
const hashInBase64 = hash.toString("base64"); // returns X+CE7kI/9+DHcJ6UN87onQ==
2020-06-06 12:36:34 -04:00
```
### Supported algorithms
Following algorithms are supported.
- md2
- md4
- md5
- ripemd160
- ripemd320
- sha1
- sha224
- sha256
- sha384
- sha512
- sha3-224
- sha3-256
- sha3-384
- sha3-512
- keccak224
- keccak256
- keccak384
- keccak512