File: serial_hasher.cc

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 (46 lines) | stat: -rw-r--r-- 971 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
#include "merkletree/serial_hasher.h"

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

using std::string;
using std::unique_ptr;

const size_t Sha256Hasher::kDigestSize = SHA256_DIGEST_LENGTH;

Sha256Hasher::Sha256Hasher() : initialized_(false) {
}

void Sha256Hasher::Reset() {
  SHA256_Init(&ctx_);
  initialized_ = true;
}

void Sha256Hasher::Update(const std::string& data) {
  if (!initialized_)
    Reset();

  SHA256_Update(&ctx_, data.data(), data.size());
}

string Sha256Hasher::Final() {
  if (!initialized_)
    Reset();

  unsigned char hash[SHA256_DIGEST_LENGTH];
  SHA256_Final(hash, &ctx_);
  initialized_ = false;
  return string(reinterpret_cast<char*>(hash), SHA256_DIGEST_LENGTH);
}

unique_ptr<SerialHasher> Sha256Hasher::Create() const {
  return unique_ptr<SerialHasher>(new Sha256Hasher);
}

// static
string Sha256Hasher::Sha256Digest(const string& data) {
  Sha256Hasher hasher;
  hasher.Reset();
  hasher.Update(data);
  return hasher.Final();
}