aboutsummaryrefslogtreecommitdiff
path: root/libbutl/sha256.cxx
blob: 95987ec503ca935deb84626f63cae4b4e37732ca (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// file      : libbutl/sha256.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbutl/sha256.hxx>

// C interface for sha256c.
//
#include <stdint.h>
#include <stddef.h> // size_t

struct SHA256_CTX
{
  uint32_t state[8];
  uint64_t count;
  uint8_t buf[64];
};

extern "C"
{
  static void SHA256_Init (SHA256_CTX*);
  static void SHA256_Update (SHA256_CTX*, const void*, size_t);
  static void SHA256_Final (uint8_t[32], SHA256_CTX*);

#include "sha256c.c"
}

#include <cctype>    // isxdigit()
#include <cassert>
#include <istream>
#include <stdexcept> // invalid_argument

#include <libbutl/utility.hxx>      // *case()
#include <libbutl/bufstreambuf.hxx>

using namespace std;

namespace butl
{
  void sha256::
  reset ()
  {
    SHA256_Init (reinterpret_cast<SHA256_CTX*> (buf_));
    done_ = false;
    empty_ = true;
  }

  void sha256::
  append (const void* b, size_t n)
  {
    if (n != 0)
    {
      SHA256_Update (reinterpret_cast<SHA256_CTX*> (buf_), b, n);

      if (empty_)
        empty_ = false;
    }
  }

  void sha256::
  append (istream& is)
  {
    bufstreambuf* buf (dynamic_cast<bufstreambuf*> (is.rdbuf ()));
    assert (buf != nullptr);

    while (is.peek () != istream::traits_type::eof () && is.good ())
    {
      size_t n (buf->egptr () - buf->gptr ());
      append (buf->gptr (), n);
      buf->gbump (static_cast<int> (n));
    }
  }

  const sha256::digest_type& sha256::
  binary () const
  {
    if (!done_)
    {
      SHA256_Final (bin_, reinterpret_cast<SHA256_CTX*> (buf_));
      done_ = true;
      buf_[0] = '\0'; // Indicate we haven't computed the string yet.
    }

    return bin_;
  }

  static const char hex_map[16] = {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'a', 'b', 'c', 'd', 'e', 'f'};

  const char* sha256::
  string () const
  {
    if (!done_)
      binary ();

    if (buf_[0] == '\0')
    {
      for (size_t i (0); i != 32; ++i)
      {
        buf_[i * 2]     = hex_map[bin_[i] >> 4];
        buf_[i * 2 + 1] = hex_map[bin_[i] & 0x0f];
      }

      buf_[64] = '\0';
    }

    return buf_;
  }

  string
  sha256_to_fingerprint (const string& s)
  {
    auto bad = []() {throw invalid_argument ("invalid SHA256 string");};

    size_t n (s.size ());
    if (n != 64)
      bad ();

    string f;
    f.reserve (n + 31);
    for (size_t i (0); i != n; ++i)
    {
      char c (s[i]);
      if (!isxdigit (c))
        bad ();

      if (i > 0 && i % 2 == 0)
        f += ":";

      f += ucase (c);
    }

    return f;
  }

  string
  fingerprint_to_sha256 (const string& f, size_t rn)
  {
    auto bad = []() {throw invalid_argument ("invalid fingerprint");};

    size_t n (f.size ());
    if (n != 32 * 3 - 1)
      bad ();

    if (rn > 64)
      rn = 64;

    string s;
    s.reserve (rn);

    // Note that we continue to validate the fingerprint after the result is
    // ready.
    //
    for (size_t i (0); i != n; ++i)
    {
      char c (f[i]);
      if ((i + 1) % 3 == 0)
      {
        if (c != ':')
          bad ();
      }
      else
      {
        if (!isxdigit (c))
          bad ();

        if (s.size () != rn)
          s += lcase (c);
      }
    }

    return s;
  }
}