File: encryption_key.hh

package info (click to toggle)
zbackup 1.5-4
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 868 kB
  • sloc: cpp: 6,957; ansic: 468; python: 207; makefile: 10
file content (51 lines) | stat: -rw-r--r-- 1,495 bytes parent folder | download | duplicates (2)
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
// Copyright (c) 2012-2014 Konstantin Isakov <ikm@zbackup.org> and ZBackup contributors, see CONTRIBUTORS
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE

#ifndef ENCRYPTION_KEY_HH_INCLUDED
#define ENCRYPTION_KEY_HH_INCLUDED

#include <exception>
#include <string>

#include "ex.hh"
#include "zbackup.pb.h"

using std::string;

class EncryptionKey
{
  bool isSet;
  unsigned const static KeySize = 16; // TODO: make this configurable
  char key[ KeySize ];

public:
  DEF_EX( exInvalidPassword, "Invalid password specified", std::exception )

  /// Decodes the encryption key from the given info and password. If info is
  /// passed as NULL, the password is ignored and no key is set
  EncryptionKey( string const & password, EncryptionKeyInfo const * );
  ~EncryptionKey();

  /// Returns true if key was set, false otherwise.
  bool hasKey() const
  { return isSet; }

  /// Returns the key. Check if there is one with hasKey() first. Note: the key
  /// should not be copied, as it may be allocated in a locked page in the
  /// future
  void const * getKey() const
  { return key; }

  /// Returns key size, in bytes
  unsigned getKeySize() const
  { return sizeof( key ); }

  /// Generates new key info using the given password
  static void generate( string const & password, EncryptionKeyInfo &,
                        EncryptionKey & encryptionkey );

  /// Returns a static instance without any key set
  static EncryptionKey const & noKey();
};

#endif