Hashing sensitive data in Rust with argon2rs

Argon2 is the password hashing algorithm that won the Password Hashing Competition in 2015. It is comprised of 2 main versions: Argon2i, which is optimised to resist side-channel attacks; and the version we’re interested in, Argon2d, which is optimised to resist GPU cracking attacks.

The argon2rs crate is a pure rust implementation of Argon2, which supports both of the above versions.

We’ll be using argon2rs’ default values for the number of passes etc., so the only things we need to supply are the data to hash and salt. If you’re not familiar, “salt” refers to random data used in one-way hashing functions — i.e. hashing functions that are too expensive to invert to be practical for attackers — to defend against dictionary attacks and pre-computed rainbow table attacks.

A common mistake when hashing data is using the same salt every time. By doing so, all same-value data that is input yields the same hashed value in the database, effectively negating the work the salt was there to do in the first place. For example, if 2 users use rustacean as their password then the value of password_hash in the database will be the same for both users in the database. Not good.

Generating a unique salt each time is fine and dandy, but at some point we’re going to need to verify the hashed data, say a password, against data the user has input into the system. When that time comes, we’re going to need access to the random salt that was used for the original hash, meaning we need to store it in the database too.

I’m sure you can see the problem with this. By storing salt in the database, we’re back in a situation where using salt in the first place becomes pointless should the database become compromised.

The solution is to not actually hash the data with the random salt directly, but to first hash the random salt with static salt stored locally, then hash the data with the hashed value of the random salt.

In doing this, both the database and the file system would need to be compromised for the aforementioned kinds of attack to be practical.

Enough theory then, let’s take a look at how to use the argon2rs crate to achieve this.


We’re going to have 3 dependencies: argon2rs, of course, for hashing; dotenv for storing a local salt; and rand for generating random salt. Add them to your Cargo.toml:

1[dependencies]
2argon2rs = "0.2"
3dotenv = "0.10"
4rand = "0.3"

Let’s start by setting up a local salt. At the highest level in your project’s directory, create a file named .env and add a LOCAL_SALT environment variable. Make sure you add .env to your .gitignore too.

1LOCAL_SALT=TWQA38DcB6Gyt4l7CG5W£lkj234WKKXP

With that done, we can setup the dotenv crate in main.rs:

1extern crate dotenv;
2
3use dotenv::dotenv;
4use std::env;
5
6fn main() {
7 dotenv().ok();
8
9 hash_data();
10}
11
12fn hash_data() {
13 let local_salt = env::var("LOCAL_SALT").expect("LOCAL_SALT must be set");
14
15 println!("local salt: {}", local_salt);
16}

Next, we’ll setup random salt generation using the rand crate:

1extern crate dotenv;
2extern crate rand;
3
4use dotenv::dotenv;
5use rand::Rng;
6use std::env;
7
8fn main() { // Unchanged… }
9
10fn hash_data() {
11 let local_salt = env::var("LOCAL_SALT").expect("LOCAL_SALT must be set");
12 let random_salt = rand::thread_rng().gen_ascii_chars().take(32).collect::<String>();
13
14 println!("local salt: {}", local_salt);
15 println!("random salt: {}", random_salt);
16}

Now we can configure argon2rs to hash the data:

1extern crate argon2rs;
2extern crate dotenv;
3extern crate rand;
4
5use argon2rs::defaults::{KIB, LANES, PASSES};
6use argon2rs::verifier::Encoded;
7use argon2rs::{Argon2, Variant};
8use dotenv::dotenv;
9use rand::Rng;
10use std::env;
11
12fn main() {
13 dotenv().ok();
14
15 let data_to_hash = /* However it is you receive user input */;
16 hash_data(data_to_hash);
17}
18
19fn hash_data(data: &str) {
20 let local_salt = env::var("LOCAL_SALT").expect("LOCAL_SALT must be set");
21 let random_salt = rand::thread_rng().gen_ascii_chars().take(32).collect::<String>();
22
23 let a2 = Argon2::new(PASSES, LANES, KIB, Variant::Argon2d).unwrap();
24 let random_salt_hash = Encoded::new(a2, random_salt.as_bytes(), local_salt.as_bytes(), b"", b"").to_u8();
25 let random_salt_hash_storable_encoding = String::from_utf8(random_salt_hash).unwrap();
26
27 let a2 = Argon2::new(PASSES, LANES, KIB, Variant::Argon2d).unwrap();
28 let data_hash = Encoded::new(a2, data.as_bytes(), random_salt_hash_storable_encoding.as_bytes(), b"", b"").to_u8();
29 let data_hash_storable_encoding = String::from_utf8(data_hash).unwrap();
30
31 println!("random salt: {}", random_salt);
32 println!("data hash: {}", data_hash_storable_encoding);
33}

At this point, we’ve finished preparing the data for the database. I’m just printing the values, but you’ll want to take the values of data_hash_storable_encoding and random_salt and add them to whatever it concerns in whichever database system you’re using.

Now to add a way to compare user-inputted data to the hash in the database:

1extern crate argon2rs;
2extern crate dotenv;
3extern crate rand;
4
5use argon2rs::defaults::{KIB, LANES, PASSES};
6use argon2rs::verifier::Encoded;
7use argon2rs::{Argon2, Variant};
8use dotenv::dotenv;
9use rand::Rng;
10use std::env;
11
12fn main() {
13 dotenv().ok();
14
15 let data_to_hash = /* However it is you receive user input */;
16 hash_data(data_to_hash);
17
18 let user_input = /* However it is you receive user input */;
19 let hashed_data = /* Retrieve the previously hashed data from your database */;
20 let random_salt = /* Retrieve the randomly generated salt from your database */;
21 compare_input_to_hashed_value(user_input, hashed_data, random_salt);
22}
23
24fn hash_data(data: &str) { // Unchanged… }
25
26fn compare_input_to_hashed_value(input: &str, hashed_value: &str, random_salt: &str) {
27 let local_salt = env::var("LOCAL_SALT").expect("LOCAL_SALT must be set");
28
29 let a2 = Argon2::new(PASSES, LANES, KIB, Variant::Argon2d).unwrap();
30 let random_salt_hash = Encoded::new(a2, random_salt.as_bytes(), local_salt.as_bytes(), b"", b"").to_u8();
31 let random_salt_hash_storable_encoding = String::from_utf8(random_salt_hash).unwrap();
32
33 let a2 = Argon2::new(PASSES, LANES, KIB, Variant::Argon2d).unwrap();
34 let data_hash = Encoded::new(a2, input.as_bytes(), random_salt_hash_storable_encoding.as_bytes(), b"", b"").to_u8();
35 let data_hash_storable_encoding = String::from_utf8(data_hash).unwrap();
36
37 if data_hash_storable_encoding == hashed_value {
38 println!("They're equal!");
39 } else {
40 println!("They're not equal.");
41 }
42}

And there we have it. You should now be able to pass the same data into this system twice and wind up with 2 different hash values in your database.

I’m no security expert, but from what I’ve gleaned in my research this approach to hashing is a good one. If you are an expert and find something wrong with it, please do reach out. I’ll update this post accordingly.

Happy Rusting.