]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/sha2.h
Merge commit 'e2073b0276226b625897ef475f225bf8f508719e' as 'triehash'
[apt.git] / apt-pkg / contrib / sha2.h
CommitLineData
84a0890e
MV
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
84a0890e 17#include <cstring>
84a0890e
MV
18
19#include "sha2_internal.h"
7ac56f8f 20#include "hashsum_template.h"
84a0890e 21
453b82a3
DK
22#ifndef APT_10_CLEANER_HEADERS
23#include <string>
24#include <algorithm>
25#include <stdint.h>
26#endif
27
28
7ac56f8f
MV
29typedef HashSumValue<512> SHA512SumValue;
30typedef HashSumValue<256> SHA256SumValue;
84a0890e 31
c31c1dde 32class SHA2SummationBase : public SummationImplementation
31693a8f
MV
33{
34 protected:
35 bool Done;
36 public:
644478e8 37 bool Add(const unsigned char *inbuf, unsigned long long len) APT_OVERRIDE APT_NONNULL(2) = 0;
31693a8f 38
31693a8f
MV
39 void Result();
40};
41
42class SHA256Summation : public SHA2SummationBase
84a0890e 43{
7ac56f8f
MV
44 SHA256_CTX ctx;
45 unsigned char Sum[32];
84a0890e
MV
46
47 public:
644478e8 48 bool Add(const unsigned char *inbuf, unsigned long long len) APT_OVERRIDE APT_NONNULL(2)
31693a8f
MV
49 {
50 if (Done)
51 return false;
52 SHA256_Update(&ctx, inbuf, len);
53 return true;
54 };
c31c1dde
DK
55 using SummationImplementation::Add;
56
31693a8f
MV
57 SHA256SumValue Result()
58 {
59 if (!Done) {
60 SHA256_Final(Sum, &ctx);
61 Done = true;
62 }
63 SHA256SumValue res;
64 res.Set(Sum);
65 return res;
66 };
4ccd3b3c 67 SHA256Summation()
31693a8f
MV
68 {
69 SHA256_Init(&ctx);
70 Done = false;
4ccd3b3c 71 memset(&Sum, 0, sizeof(Sum));
31693a8f 72 };
84a0890e
MV
73};
74
31693a8f 75class SHA512Summation : public SHA2SummationBase
84a0890e 76{
7ac56f8f
MV
77 SHA512_CTX ctx;
78 unsigned char Sum[64];
84a0890e
MV
79
80 public:
644478e8 81 bool Add(const unsigned char *inbuf, unsigned long long len) APT_OVERRIDE APT_NONNULL(2)
31693a8f
MV
82 {
83 if (Done)
84 return false;
85 SHA512_Update(&ctx, inbuf, len);
86 return true;
87 };
c31c1dde
DK
88 using SummationImplementation::Add;
89
31693a8f
MV
90 SHA512SumValue Result()
91 {
92 if (!Done) {
93 SHA512_Final(Sum, &ctx);
94 Done = true;
95 }
96 SHA512SumValue res;
97 res.Set(Sum);
98 return res;
99 };
100 SHA512Summation()
101 {
102 SHA512_Init(&ctx);
103 Done = false;
4ccd3b3c 104 memset(&Sum, 0, sizeof(Sum));
31693a8f 105 };
84a0890e 106};
54ce88fd 107
7ac56f8f 108
54ce88fd 109#endif