Skip to content

Commit 91bec6e

Browse files
committed
Password hashing
Closes #13
1 parent 67d6c63 commit 91bec6e

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Examples showing how to do many things in Gleam!
1515
## Cryptography
1616

1717
- [Hashing data](./universal/test/cryptography/hashing_data.gleam)
18+
- [Hashing passwords](./universal/test/cryptography/hashing_passwords.gleam) (Erlang)
1819

1920
## Data structures
2021

erlang/gleam.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ target = "erlang"
55
[dependencies]
66
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
77
youid = ">= 1.2.0 and < 2.0.0"
8+
argus = ">= 1.0.1 and < 2.0.0"
89

910
[dev-dependencies]
1011
gleeunit = ">= 1.0.0 and < 2.0.0"

erlang/manifest.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
# You typically do not need to edit this file
33

44
packages = [
5+
{ name = "argus", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "jargon"], otp_app = "argus", source = "hex", outer_checksum = "8AD8E412B8A7223A2393CA09F8D54A0FC62CB3D4D6BBDF40635C904F9EC1C82C" },
56
{ name = "gleam_crypto", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "ADD058DEDE8F0341F1ADE3AAC492A224F15700829D9A3A3F9ADF370F875C51B7" },
67
{ name = "gleam_erlang", version = "0.26.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "3DF72F95F4716883FA51396FB0C550ED3D55195B541568CAF09745984FD37AD1" },
78
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
89
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
10+
{ name = "jargon", version = "1.0.0", build_tools = ["rebar3"], requirements = [], otp_app = "jargon", source = "hex", outer_checksum = "60FBFACC920EAEBC96C76DA3D8ED814FABDDC2103CC0D04FE314A3C15F3174DF" },
911
{ name = "youid", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_stdlib"], otp_app = "youid", source = "hex", outer_checksum = "EF0F693004E221155EE5909C6D3C945DD14F7117DBA882887CF5F45BE399B8CA" },
1012
]
1113

1214
[requirements]
15+
argus = { version = ">= 1.0.1 and < 2.0.0" }
1316
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
1417
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
1518
youid = { version = ">= 1.2.0 and < 2.0.0" }
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//// # Hashing passwords
2+
////
3+
//// The argus library has bindings to the standard C implementation of the
4+
//// argon2 password hashing algorithm. This is the best option for password
5+
//// hashing presently. Currently it only supports Erlang.
6+
////
7+
//// Because argus depends on C code you will need to ensure that the computers
8+
//// you compile your project on have a C compiler and Make installed.
9+
////
10+
//// ## Dependencies
11+
////
12+
//// - https://hex.pm/packages/argus
13+
14+
import argus
15+
import gleeunit/should
16+
17+
pub fn main_test() {
18+
let password = "blink182"
19+
20+
// The `hash` function can be used to hash a password with a salt.
21+
//
22+
// A SALT MUST NEVER BE REUSED. Generate a new one for each password you
23+
// need to hash.
24+
let assert Ok(hashes) =
25+
argus.hasher()
26+
|> argus.hash(password, argus.gen_salt())
27+
28+
// The `verify` function is used to check a password against a hash.
29+
argus.verify(hashes.encoded_hash, password)
30+
|> should.equal(Ok(True))
31+
32+
argus.verify(hashes.encoded_hash, "some-other-password")
33+
|> should.equal(Ok(False))
34+
35+
// Password hashing is a (deliberately) expensive operation.
36+
// If you are hashing passwords frequently in your test suite you may want to
37+
// configure the hasher to less expensive to run.
38+
//
39+
// NEVER DO THIS OUTSIDE OF TESTS. You must do your research before setting
40+
// any of these values and ensure you know what you are doing.
41+
let assert Ok(_hashes) =
42+
argus.hasher()
43+
|> argus.algorithm(argus.Argon2id)
44+
|> argus.time_cost(3)
45+
|> argus.memory_cost(12_228)
46+
|> argus.parallelism(1)
47+
|> argus.hash_length(32)
48+
|> argus.hash("password", argus.gen_salt())
49+
}

0 commit comments

Comments
 (0)