1 /*******************************************************************************
2 
3     PBKDF2 key derivation wrapper.
4 
5     Requires linking with libgcrypt:
6             -L-lgcrypt
7 
8     Copyright:
9         Copyright (c) 2009-2016 dunnhumby Germany GmbH.
10         All rights reserved.
11 
12     License:
13         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
14         Alternatively, this file may be distributed under the terms of the Tango
15         3-Clause BSD License (see LICENSE_BSD.txt for details).
16 
17 *******************************************************************************/
18 
19 module ocean.util.cipher.gcrypt.PBKDF2;
20 
21 
22 import ocean.util.cipher.gcrypt.core.KeyDerivationCore;
23 
24 /*******************************************************************************
25 
26     PBKDF2 with SHA256 hashing wrapper class
27 
28     For a usage example, see the unittests below.
29 
30 *******************************************************************************/
31 
32 public alias KeyDerivationCore!(KDF.GCRY_KDF_PBKDF2, Hasher.GCRY_MD_SHA256) PBKDF2;
33 
34 version ( UnitTest )
35 {
36     import ocean.transition;
37     import ocean.core.Test;
38     import ocean.text.convert.Hex;
39 }
40 
41 /// PBKDF2 usage example
42 unittest
43 {
44     // Set up the passphrase and salt
45     auto passphrase = cast(Immut!(ubyte)[])"passphrase";
46     auto salt = cast(Immut!(ubyte)[])"salt";
47 
48     // Create the key derivation instance
49     auto pbkdf2 = new PBKDF2(passphrase, salt);
50 
51     // The number of hashing iterations
52     static immutable ITERATIONS = 256;
53 
54     // The buffer to write the key to, set to the expected key length
55     ubyte[] key_buf;
56     key_buf.length = 32;
57 
58     // Derive the key
59     auto key = pbkdf2.derive(ITERATIONS, key_buf);
60 
61     // The expected key is created so the output of pbkdf2 can be verified
62     static immutable EXPECTED_KEY = "1a0e45a1b7dd26f47b3549c56dca01df2fa27fa50ef799d9165db53b202fa267";
63     ubyte[] expected_key;
64     hexToBin(EXPECTED_KEY, expected_key);
65 
66     test!("==")(key, expected_key);
67 }