Botan 2.19.5 Crypto and TLS for C&

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.h>
8#include <botan/database.h>
9
10 namespace Botan {
11
12 Encrypted_PSK_Database_SQL::Encrypted_PSK_Database_SQL(const secure_vector<uint8_t>& master_key,
13 std::shared_ptr<SQL_Database> db,
14 const std::string& table_name) :
15 Encrypted_PSK_Database(master_key),
16 m_db(db),
17 m_table_name(table_name)
18 {
19 m_db->create_table(
20 "create table if not exists " + m_table_name +
21 "(psk_name TEXT PRIMARY KEY, psk_value TEXT)");
22 }
23
24 Encrypted_PSK_Database_SQL::~Encrypted_PSK_Database_SQL()
25 {
26 /* for ~unique_ptr */
27 }
28
29 void Encrypted_PSK_Database_SQL::kv_del(const std::string& name)
30 {
31 auto stmt = m_db->new_statement("delete from " + m_table_name + " where psk_name=?1");
32 stmt->bind(1, name);
33 stmt->spin();
34 }
35
36 void Encrypted_PSK_Database_SQL::kv_set(const std::string& name, const std::string& value)
37 {
38 auto stmt = m_db->new_statement("insert or replace into " + m_table_name + " values(?1, ?2)");
39
40 stmt->bind(1, name);
41 stmt->bind(2, value);
42
43 stmt->spin();
44 }
45
46std::string Encrypted_PSK_Database_SQL::kv_get(const std::string& name) const
47 {
48 auto stmt = m_db->new_statement("select psk_value from " + m_table_name +
49 " where psk_name = ?1");
50
51 stmt->bind(1, name);
52
53 while(stmt->step())
54 {
55 return stmt->get_str(0);
56 }
57 return "";
58 }
59
60std::set<std::string> Encrypted_PSK_Database_SQL::kv_get_all() const
61 {
62 std::set<std::string> names;
63
64 auto stmt = m_db->new_statement("select psk_name from " + m_table_name);
65
66 while(stmt->step())
67 {
68 names.insert(stmt->get_str(0));
69 }
70
71 return names;
72 }
73
74}
75
Botan::Encrypted_PSK_Database_SQL::~Encrypted_PSK_Database_SQL
~Encrypted_PSK_Database_SQL()
Definition: psk_db_sql.cpp:24
Botan::Encrypted_PSK_Database_SQL::Encrypted_PSK_Database_SQL
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:12
Botan::Encrypted_PSK_Database
Definition: psk_db.h:91
name
std::string name
Definition: commoncrypto_hash.cpp:24
Botan
Definition: alg_id.cpp:13
Botan::secure_vector
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65