]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/sha2.h
releasing package apt version 0.9.13~exp1
[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
17#include <string>
18#include <cstring>
19#include <algorithm>
20#include <stdint.h>
21
22#include "sha2_internal.h"
7ac56f8f 23#include "hashsum_template.h"
84a0890e 24
7ac56f8f
MV
25typedef HashSumValue<512> SHA512SumValue;
26typedef HashSumValue<256> SHA256SumValue;
84a0890e 27
c31c1dde 28class SHA2SummationBase : public SummationImplementation
31693a8f
MV
29{
30 protected:
31 bool Done;
32 public:
650faab0 33 bool Add(const unsigned char *inbuf, unsigned long long len) = 0;
31693a8f 34
31693a8f
MV
35 void Result();
36};
37
38class SHA256Summation : public SHA2SummationBase
84a0890e 39{
7ac56f8f
MV
40 SHA256_CTX ctx;
41 unsigned char Sum[32];
84a0890e
MV
42
43 public:
650faab0 44 bool Add(const unsigned char *inbuf, unsigned long long len)
31693a8f
MV
45 {
46 if (Done)
47 return false;
48 SHA256_Update(&ctx, inbuf, len);
49 return true;
50 };
c31c1dde
DK
51 using SummationImplementation::Add;
52
31693a8f
MV
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 };
4ccd3b3c 63 SHA256Summation()
31693a8f
MV
64 {
65 SHA256_Init(&ctx);
66 Done = false;
4ccd3b3c 67 memset(&Sum, 0, sizeof(Sum));
31693a8f 68 };
84a0890e
MV
69};
70
31693a8f 71class SHA512Summation : public SHA2SummationBase
84a0890e 72{
7ac56f8f
MV
73 SHA512_CTX ctx;
74 unsigned char Sum[64];
84a0890e
MV
75
76 public:
650faab0 77 bool Add(const unsigned char *inbuf, unsigned long long len)
31693a8f
MV
78 {
79 if (Done)
80 return false;
81 SHA512_Update(&ctx, inbuf, len);
82 return true;
83 };
c31c1dde
DK
84 using SummationImplementation::Add;
85
31693a8f
MV
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 };
96 SHA512Summation()
97 {
98 SHA512_Init(&ctx);
99 Done = false;
4ccd3b3c 100 memset(&Sum, 0, sizeof(Sum));
31693a8f 101 };
84a0890e 102};
54ce88fd 103
7ac56f8f 104
54ce88fd 105#endif