aboutsummaryrefslogtreecommitdiff
path: root/libbutl/sha1.mxx
blob: ea87367606bb052ccd541b156d5d38b3bc81eecd (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
// file      : libbutl/sha1.mxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_modules_ts
#pragma once
#endif

// C includes.

#ifndef __cpp_lib_modules_ts
#include <string>
#include <cstddef>     // size_t
#include <cstdint>
#include <cstring>     // strlen()
#endif

// Other includes.

#ifdef __cpp_modules_ts
export module butl.sha1;
#ifdef __cpp_lib_modules_ts
import std.core;
#endif
#endif

#include <libbutl/export.hxx>

LIBBUTL_MODEXPORT namespace butl
{
  class ifdstream;

  // SHA1 checksum calculator.
  //
  // For a single chunk of data a sum can be obtained in one line, for
  // example:
  //
  // cerr << sha1 ("123").string () << endl;
  //
  class LIBBUTL_SYMEXPORT sha1
  {
  public:
    sha1 () {reset ();}

    // Append binary data.
    //
    void
    append (const void*, std::size_t);

    sha1 (const void* b, std::size_t n): sha1 () {append (b, n);}

    // Append string.
    //
    // Note that the hash includes the '\0' terminator. Failed that, a call
    // with an empty string will be indistinguishable from no call at all.
    //
    void
    append (const std::string& s) {append (s.c_str (), s.size () + 1);}

    void
    append (const char* s) {append (s, std::strlen (s) + 1);}

    explicit
    sha1 (const std::string& s): sha1 () {append (s);}

    explicit
    sha1 (const char* s): sha1 () {append (s);}

    // Append stream.
    //
    void
    append (ifdstream&);

    explicit
    sha1 (ifdstream& i): sha1 () {append (i);}

    // Check if any data has been hashed.
    //
    bool
    empty () const {return empty_;}

    // Reset to the default-constructed state.
    //
    void
    reset ();

    // Extract result.
    //
    // It can be obtained as either a 20-byte binary digest or as a 40-
    // character hex-encoded C-string.
    //
    using digest_type = std::uint8_t[20];

    const digest_type&
    binary () const;

    const char*
    string () const;

    std::string
    abbreviated_string (std::size_t n) const
    {
      return std::string (string (), n < 40 ? n : 40);
    }

  private:
    struct context // Note: identical to SHA1_CTX.
    {
      union {
        std::uint8_t b8[20];
        std::uint32_t b32[5];
      } h;
      union {
        std::uint8_t b8[8];
        std::uint64_t b64[1];
      } c;
      union {
        std::uint8_t b8[64];
        std::uint32_t b32[16];
      } m;
      std::uint8_t count;
    };

    union
    {
      mutable context ctx_;
      mutable char buf_[sizeof (context)]; // Also used to store string rep.
    };

    mutable digest_type bin_;
    mutable bool done_;
    bool empty_;
  };
}