summaryrefslogtreecommitdiffstats
path: root/comm/third_party/botan/src/cli/argon2.cpp
blob: 2b07027c3398ac453ae2c43f6795b88fa2bb1756 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* (C) 2019 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "cli.h"

#if defined(BOTAN_HAS_ARGON2)
   #include <botan/argon2.h>
#endif

namespace Botan_CLI {

#if defined(BOTAN_HAS_ARGON2)

class Generate_Argon2 final : public Command
   {
   public:
      Generate_Argon2() : Command("gen_argon2 --mem=65536 --p=1 --t=1 password") {}

      std::string group() const override
         {
         return "passhash";
         }

      std::string description() const override
         {
         return "Calculate Argon2 password hash";
         }

      void go() override
         {
         const std::string password = get_passphrase_arg("Passphrase to hash", "password");
         const size_t M = get_arg_sz("mem");
         const size_t p = get_arg_sz("p");
         const size_t t = get_arg_sz("t");

         output() << Botan::argon2_generate_pwhash(password.data(), password.size(), rng(), p, M, t) << "\n";
         }
   };

BOTAN_REGISTER_COMMAND("gen_argon2", Generate_Argon2);

class Check_Argon2 final : public Command
   {
   public:
      Check_Argon2() : Command("check_argon2 password hash") {}

      std::string group() const override
         {
         return "passhash";
         }

      std::string description() const override
         {
         return "Verify Argon2 password hash";
         }

      void go() override
         {
         const std::string password = get_passphrase_arg("Password to check", "password");
         const std::string hash = get_arg("hash");

         const bool ok = Botan::argon2_check_pwhash(password.data(), password.size(), hash);

         output() << "Password is " << (ok ? "valid" : "NOT valid") << std::endl;

         if(ok == false)
            set_return_code(1);
         }
   };

BOTAN_REGISTER_COMMAND("check_argon2", Check_Argon2);

#endif // argon2

}