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