Botan  2.7.0
Crypto and TLS for C++11
psk_db_sql.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2017 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #include <botan/psk_db_sql.h>
8 
9 namespace Botan {
10 
12  std::shared_ptr<SQL_Database> db,
13  const std::string& table_name) :
14  Encrypted_PSK_Database(master_key),
15  m_db(db),
16  m_table_name(table_name)
17  {
18  m_db->create_table(
19  "create table if not exists " + m_table_name +
20  "(psk_name TEXT PRIMARY KEY, psk_value TEXT)");
21  }
22 
23 void Encrypted_PSK_Database_SQL::kv_del(const std::string& name)
24  {
25  auto stmt = m_db->new_statement("delete from " + m_table_name + " where psk_name=?1");
26  stmt->bind(1, name);
27  stmt->spin();
28  }
29 
30 void Encrypted_PSK_Database_SQL::kv_set(const std::string& name, const std::string& value)
31  {
32  auto stmt = m_db->new_statement("insert or replace into " + m_table_name + " values(?1, ?2)");
33 
34  stmt->bind(1, name);
35  stmt->bind(2, value);
36 
37  stmt->spin();
38  }
39 
40 std::string Encrypted_PSK_Database_SQL::kv_get(const std::string& name) const
41  {
42  auto stmt = m_db->new_statement("select psk_value from " + m_table_name +
43  " where psk_name = ?1");
44 
45  stmt->bind(1, name);
46 
47  while(stmt->step())
48  {
49  return stmt->get_str(0);
50  }
51  return "";
52  }
53 
54 std::set<std::string> Encrypted_PSK_Database_SQL::kv_get_all() const
55  {
56  std::set<std::string> names;
57 
58  auto stmt = m_db->new_statement("select psk_name from " + m_table_name);
59 
60  while(stmt->step())
61  {
62  names.insert(stmt->get_str(0));
63  }
64 
65  return names;
66  }
67 
68 }
69 
Encrypted_PSK_Database_SQL(const secure_vector< uint8_t > &master_key, std::shared_ptr< SQL_Database > db, const std::string &table_name)
Definition: psk_db_sql.cpp:11
Definition: alg_id.cpp:13
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88