1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // A SecretEncoder implementation that uses hexadecimal encoding. This encoding
16 // has 2 symbols per byte since each hex character uses 4 bits.
17 
18 #include "polo/encoding/hexadecimalencoder.h"
19 
20 #include <string>
21 #include <vector>
22 #include "polo/util/poloutil.h"
23 
24 namespace polo {
25 namespace encoding {
26 
EncodeToString(const std::vector<uint8_t> & secret) const27 std::string HexadecimalEncoder::EncodeToString(
28     const std::vector<uint8_t>& secret) const {
29   return polo::util::PoloUtil::BytesToHexString(&secret[0], secret.size());
30 }
31 
DecodeToBytes(const std::string & secret) const32 std::vector<uint8_t> HexadecimalEncoder::DecodeToBytes(
33     const std::string& secret) const {
34   uint8_t* bytes;
35   size_t length = polo::util::PoloUtil::HexStringToBytes(secret, bytes);
36   std::vector<uint8_t> decoded(bytes, bytes + length);
37   delete[] bytes;
38 
39   return decoded;
40 }
41 
symbols_per_byte() const42 size_t HexadecimalEncoder::symbols_per_byte() const {
43   return HEX_SYMBOLS_PER_BYTE;
44 }
45 
46 }  // namespace encoding
47 }  // namespace polo
48