Use locked_sstream. Replace once parse_stream with parse_memory.
[libdcp.git] / src / key.cc
1 /*
2     Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 /** @file  src/key.cc
35  *  @brief Key class.
36  */
37
38 #include "key.h"
39 #include <locked_sstream.h>
40 #include <asdcp/AS_DCP.h>
41 #include <asdcp/KM_prng.h>
42 #include <asdcp/KM_util.h>
43 #include <string>
44 #include <iomanip>
45
46 using std::string;
47 using std::setw;
48 using std::setfill;
49 using namespace dcp;
50
51 Key::Key ()
52         : _value (new uint8_t[ASDCP::KeyLen])
53 {
54         Kumu::FortunaRNG rng;
55         rng.FillRandom (_value, ASDCP::KeyLen);
56 }
57
58 Key::Key (uint8_t const * value)
59         : _value (new uint8_t[ASDCP::KeyLen])
60 {
61         memcpy (_value, value, ASDCP::KeyLen);
62 }
63
64 Key::Key (string value)
65         : _value (new uint8_t[ASDCP::KeyLen])
66 {
67         unsigned int length;
68         Kumu::hex2bin (value.c_str(), _value, ASDCP::KeyLen, &length);
69 }
70
71 Key::Key (Key const & other)
72         : _value (new uint8_t[ASDCP::KeyLen])
73 {
74         memcpy (_value, other._value, ASDCP::KeyLen);
75 }
76
77 Key::~Key ()
78 {
79         delete[] _value;
80 }
81
82 Key &
83 Key::operator= (Key const & other)
84 {
85         if (this == &other) {
86                 return *this;
87         }
88
89         memcpy (_value, other._value, ASDCP::KeyLen);
90         return *this;
91 }
92
93 string
94 Key::hex () const
95 {
96         locked_stringstream g;
97
98         for (unsigned int i = 0; i < ASDCP::KeyLen; ++i) {
99                 g << setw(2) << setfill('0') << std::hex << static_cast<int> (_value[i]);
100         }
101
102         return g.str ();
103 }
104
105 bool
106 dcp::operator== (Key const & a, Key const & b)
107 {
108         return memcmp (a.value(), b.value(), ASDCP::KeyLen) == 0;
109 }
110
111 bool
112 dcp::operator!= (Key const & a, Key const & b)
113 {
114         return !(a == b);
115 }