apt  0.9.14
sha2.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: sha512.h,v 1.3 2001/05/07 05:05:47 jgg Exp $
4 /* ######################################################################
5 
6  SHA{512,256}SumValue - Storage for a SHA-{512,256} hash.
7  SHA{512,256}Summation - SHA-{512,256} Secure Hash Algorithm.
8 
9  This is a C++ interface to a set of SHA{512,256}Sum functions, that mirrors
10  the equivalent MD5 & SHA1 classes.
11 
12  ##################################################################### */
13  /*}}}*/
14 #ifndef APTPKG_SHA2_H
15 #define APTPKG_SHA2_H
16 
17 #include <string>
18 #include <cstring>
19 #include <algorithm>
20 #include <stdint.h>
21 
22 #include "sha2_internal.h"
23 #include "hashsum_template.h"
24 
27 
29 {
30  protected:
31  bool Done;
32  public:
33  bool Add(const unsigned char *inbuf, unsigned long long len) = 0;
34 
35  void Result();
36 };
37 
39 {
40  SHA256_CTX ctx;
41  unsigned char Sum[32];
42 
43  public:
44  bool Add(const unsigned char *inbuf, unsigned long long len)
45  {
46  if (Done)
47  return false;
48  SHA256_Update(&ctx, inbuf, len);
49  return true;
50  };
51  using SummationImplementation::Add;
52 
53  SHA256SumValue Result()
54  {
55  if (!Done) {
56  SHA256_Final(Sum, &ctx);
57  Done = true;
58  }
59  SHA256SumValue res;
60  res.Set(Sum);
61  return res;
62  };
64  {
65  SHA256_Init(&ctx);
66  Done = false;
67  memset(&Sum, 0, sizeof(Sum));
68  };
69 };
70 
72 {
73  SHA512_CTX ctx;
74  unsigned char Sum[64];
75 
76  public:
77  bool Add(const unsigned char *inbuf, unsigned long long len)
78  {
79  if (Done)
80  return false;
81  SHA512_Update(&ctx, inbuf, len);
82  return true;
83  };
84  using SummationImplementation::Add;
85 
86  SHA512SumValue Result()
87  {
88  if (!Done) {
89  SHA512_Final(Sum, &ctx);
90  Done = true;
91  }
92  SHA512SumValue res;
93  res.Set(Sum);
94  return res;
95  };
97  {
98  SHA512_Init(&ctx);
99  Done = false;
100  memset(&Sum, 0, sizeof(Sum));
101  };
102 };
103 
104 
105 #endif
Definition: hashsum_template.h:28
Definition: sha2_internal.h:105
Definition: sha2.h:38
Definition: sha2.h:28
Definition: sha2.h:71
Definition: sha2_internal.h:110
Definition: hashsum_template.h:97