No-op; Fix GPL address and mention libdcp by name.
[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 */
20
21 /** @file  src/key.cc
22  *  @brief Key class.
23  */
24
25 #include "key.h"
26 #include "AS_DCP.h"
27 #include "KM_prng.h"
28 #include "KM_util.h"
29 #include <sstream>
30 #include <string>
31 #include <iomanip>
32
33 using std::string;
34 using std::stringstream;
35 using std::setw;
36 using std::setfill;
37 using namespace dcp;
38
39 Key::Key ()
40         : _value (new uint8_t[ASDCP::KeyLen])
41 {
42         Kumu::FortunaRNG rng;
43         rng.FillRandom (_value, ASDCP::KeyLen);
44 }
45
46 Key::Key (uint8_t const * value)
47         : _value (new uint8_t[ASDCP::KeyLen])
48 {
49         memcpy (_value, value, ASDCP::KeyLen);
50 }
51
52 Key::Key (string value)
53         : _value (new uint8_t[ASDCP::KeyLen])
54 {
55         unsigned int length;
56         Kumu::hex2bin (value.c_str(), _value, ASDCP::KeyLen, &length);
57 }
58
59 Key::Key (Key const & other)
60         : _value (new uint8_t[ASDCP::KeyLen])
61 {
62         memcpy (_value, other._value, ASDCP::KeyLen);
63 }
64
65 Key::~Key ()
66 {
67         delete[] _value;
68 }
69
70 Key &
71 Key::operator= (Key const & other)
72 {
73         if (this == &other) {
74                 return *this;
75         }
76
77         memcpy (_value, other._value, ASDCP::KeyLen);
78         return *this;
79 }
80
81 string
82 Key::hex () const
83 {
84         stringstream g;
85
86         for (unsigned int i = 0; i < ASDCP::KeyLen; ++i) {
87                 g << setw(2) << setfill('0') << std::hex << static_cast<int> (_value[i]);
88         }
89
90         return g.str ();
91 }
92
93 bool
94 dcp::operator== (Key const & a, Key const & b)
95 {
96         return memcmp (a.value(), b.value(), ASDCP::KeyLen) == 0;
97 }
98
99 bool
100 dcp::operator!= (Key const & a, Key const & b)
101 {
102         return !(a == b);
103 }
104