File: serial_hasher.h

package info (click to toggle)
golang-github-google-certificate-transparency 0.0~git20160709.0.0f6e3d1~ds1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 5,676 kB
  • sloc: cpp: 35,278; python: 11,838; java: 1,911; sh: 1,885; makefile: 950; xml: 520; ansic: 225
file content (61 lines) | stat: -rw-r--r-- 1,548 bytes parent folder | download
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
#ifndef CERT_TRANS_MERKLETREE_SERIAL_HASHER_H_
#define CERT_TRANS_MERKLETREE_SERIAL_HASHER_H_

#include <openssl/sha.h>
#include <stddef.h>
#include <memory>
#include <string>

#include "base/macros.h"

class SerialHasher {
 public:
  SerialHasher() = default;
  virtual ~SerialHasher() = default;

  virtual size_t DigestSize() const = 0;

  // Reset the context. Must be called before the first Update() call.
  // Optionally it can be called after each Final() call; however
  // doing so is a no-op since Final() will leave the hasher in a
  // reset state.
  virtual void Reset() = 0;

  // Update the hash context with (binary) data.
  virtual void Update(const std::string& data) = 0;

  // Finalize the hash context and return the binary digest blob.
  virtual std::string Final() = 0;

  // A virtual constructor, creates a new instance of the same type.
  virtual std::unique_ptr<SerialHasher> Create() const = 0;

 private:
  DISALLOW_COPY_AND_ASSIGN(SerialHasher);
};

class Sha256Hasher : public SerialHasher {
 public:
  Sha256Hasher();

  size_t DigestSize() const {
    return kDigestSize;
  }

  void Reset();
  void Update(const std::string& data);
  std::string Final();
  std::unique_ptr<SerialHasher> Create() const;

  // Create a new hasher and call Reset(), Update(), and Final().
  static std::string Sha256Digest(const std::string& data);

 private:
  SHA256_CTX ctx_;
  bool initialized_;
  static const size_t kDigestSize;

  DISALLOW_COPY_AND_ASSIGN(Sha256Hasher);
};

#endif  // CERT_TRANS_MERKLETREE_SERIAL_HASHER_H_