Initial commit.
[BoarSSL] / Crypto / DigestCore.cs
1 /*
2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 using System;
26
27 namespace Crypto {
28
29 /*
30 * This class is a convenient base class for implementations of IDigest.
31 * Hash function implementations must implement:
32 * int DigestSize { get; }
33 * int BlockSize { get; }
34 * int PaddingOverhead { get; }
35 * void Update(byte b)
36 * void Update(byte[] buf, int off, int len)
37 * void DoPartial(byte[] outBuf, int off)
38 * void Reset()
39 * IDigest Dup()
40 *
41 * Implementations SHOULD provide overrides for:
42 * void CurrentState(byte[], int)
43 *
44 * In this class:
45 * Update(byte[]) calls Update(byte[],int,int)
46 * DoPartial() calls DoPartial(byte[],int)
47 * DoFinal() calls DoPartial() and Reset()
48 * DoFinal(byte[],int) calls DoPartial(byte[],int) and Reset()
49 */
50
51 public abstract class DigestCore : IDigest {
52
53 /* see IDigest */
54 public abstract string Name { get; }
55
56 /* see IDigest */
57 public abstract int DigestSize { get; }
58
59 /* see IDigest */
60 public abstract int BlockSize { get; }
61
62 /* see IDigest */
63 public abstract void Update(byte b);
64
65 /* see IDigest */
66 public virtual void Update(byte[] buf)
67 {
68 Update(buf, 0, buf.Length);
69 }
70
71 /* see IDigest */
72 public abstract void Update(byte[] buf, int off, int len);
73
74 /* see IDigest */
75 public abstract void DoPartial(byte[] outBuf, int off);
76
77 /* see IDigest */
78 public virtual byte[] DoPartial()
79 {
80 byte[] buf = new byte[DigestSize];
81 DoPartial(buf, 0);
82 return buf;
83 }
84
85 /* see IDigest */
86 public virtual void DoFinal(byte[] outBuf, int off)
87 {
88 DoPartial(outBuf, off);
89 Reset();
90 }
91
92 /* see IDigest */
93 public virtual byte[] DoFinal()
94 {
95 byte[] r = DoPartial();
96 Reset();
97 return r;
98 }
99
100 /* see IDigest */
101 public abstract void Reset();
102
103 /* see IDigest */
104 public abstract IDigest Dup();
105
106 /*
107 * Default implementation throws a NotSupportedException.
108 */
109 public virtual void CurrentState(byte[] outBuf, int off)
110 {
111 throw new NotSupportedException();
112 }
113
114 /* see IDigest */
115 public virtual byte[] Hash(byte[] buf)
116 {
117 return Hash(buf, 0, buf.Length);
118 }
119
120 /* see IDigest */
121 public virtual byte[] Hash(byte[] buf, int off, int len)
122 {
123 IDigest h = Dup();
124 h.Reset();
125 h.Update(buf, off, len);
126 return h.DoFinal();
127 }
128 }
129
130 }