aboutsummaryrefslogtreecommitdiff
path: root/docs/guides/util/hash-a-password.md
blob: 61a59aeaf68da2b41eb88c9614e73177dc75b564 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
---
name: Hash a password
---

The `Bun.password.hash()` function provides a fast, built-in mechanism for securely hashing passwords in Bun. No third-party dependencies are required.

```ts
const password = "super-secure-pa$$word";

const hash = await Bun.password.hash(password);
// => $argon2id$v=19$m=65536,t=2,p=1$tFq+9AVr1bfPxQdh6E8DQRhEXg/M/...
```

---

By default this uses the [Argon2id](https://en.wikipedia.org/wiki/Argon2) algorithm. Pass a second argument to `Bun.hash.password()` to use a different algorithm or configure the hashing parameters.

```ts
const password = "super-secure-pa$$word";

// use argon2 (default)
const argonHash = await Bun.password.hash(password, {
  memoryCost: 4, // memory usage in kibibytes
  timeCost: 3, // the number of iterations
});
```

---

Bun also implements the [bcrypt](https://en.wikipedia.org/wiki/Bcrypt) algorithm. Specify `algorithm: "bcrypt"` to use it.

```ts
// use bcrypt
const bcryptHash = await Bun.password.hash(password, {
  algorithm: "bcrypt",
  cost: 4, // number between 4-31
});
```

---

To verify a password, use `Bun.password.verify()`. The algorithm and its parameters are stored in the hash itself, so there's no need to re-specify any configuration.

```ts
const password = "super-secure-pa$$word";
const hash = await Bun.password.hash(password);

const isMatch = await Bun.password.verify(password, hash);
// => true
```

---

See [Docs > API > Hashing](/docs/api/hashing#bun-password) for complete documentation.
d4bf&follow=1'>Add disabled optimizationGravatar Jarred Sumner 1-0/+51 2023-02-15Add more logging to napiGravatar Jarred Sumner 1-30/+150 2023-02-15Incorrect implementation of `napi_create_threadsafe_function`Gravatar Jarred Sumner 1-21/+43 2023-02-15feat(fetch) AbortSignal (#2019)Gravatar Ciro Spaciari 17-58/+443 2023-02-15fix(webcrypto): fix ed25519 CryptoKey.algorithm (#2082)Gravatar Derrick Farris 2-9/+28 2023-02-15Fix 2063 (#2079)Gravatar Justin Whear 2-2/+11 2023-02-15Make sure we test * in tesconfigGravatar Jarred Sumner 2-1/+3 2023-02-15don't return an error thereGravatar Jarred Sumner 2-1/+3 2023-02-15Fix castGravatar Jarred Sumner 1-15/+17 2023-02-15ensure we allocate for > 6 argumentsGravatar Jarred Sumner 1-6/+13 2023-02-15Update async_hooks.exports.jsGravatar Jarred Sumner 1-2/+2 2023-02-15workaround prisma's usage of `eval("__dirname")`Gravatar Jarred Sumner 1-1/+23 2023-02-15some cleanupGravatar Jarred Sumner 2-15/+9 2023-02-15ED25519 WebCrypto (#1971)Gravatar Jarred Sumner 12-11/+1167 2023-02-14Fix up async_hooks polyfillGravatar Jarred Sumner 2-8/+63 2023-02-14Add temporary polyfill for async_hooksGravatar Jarred Sumner 5-108/+324 2023-02-14:mask: async_hooksGravatar Jarred Sumner 1-0/+4 2023-02-14[install] link network-delayed `.bin` scripts correctly (#2076)Gravatar Alex Lam S.L 3-16/+21 2023-02-14don't break esbuildGravatar Jarred Sumner 7-75/+50 2023-02-14Add workaround for `tls` and `worker_threads`Gravatar Jarred Sumner 3-1/+64 2023-02-14[install] improve `package.json` validation (#2074)Gravatar Alex Lam S.L 6-104/+342 2023-02-14[WIP] fix(node:fs): export `fs.ReadStream` and `fs.WriteStream` (#1798)Gravatar Derrick Farris 4-72/+326 2023-02-14Reject with error when invalid fetch() body (#2047)Gravatar Eric Zhang 2-12/+44 2023-02-13fix(FormData): make String explicit, thanks @dylan-conway (#2065)Gravatar Derrick Farris 1-1/+1 2023-02-13fix(FormData): add string literal operator (#2064)Gravatar Derrick Farris 1-2/+2 2023-02-13Add pretty printer for FormDataGravatar Jarred Sumner 5-1/+101 2023-02-13Add dynamic port assigning to Bun.serve (#2062)Gravatar Michał Warda 3-5/+40 2023-02-13feat(napi): add `napi_get_value_bigint_words` (#2061)Gravatar Derrick Farris 3-0/+44 2023-02-13Fixes https://github.com/oven-sh/bun/issues/1456Gravatar Jarred Sumner 8-1/+148