Initial commit.
[BoarSSL] / Crypto / IDigest.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 interface qualifies a hash function implementation.
31 */
32
33 public interface IDigest {
34
35 /*
36 * Get the hash function symbolic name.
37 */
38 string Name { get; }
39
40 /*
41 * Get the hash function output size, in bytes.
42 */
43 int DigestSize { get; }
44
45 /*
46 * Get the hash function block size, in bytes (the block
47 * size is used in the definition of HMAC).
48 */
49 int BlockSize { get; }
50
51 /*
52 * Add one byte to the current input.
53 */
54 void Update(byte b);
55
56 /*
57 * Add some bytes to the current input.
58 */
59 void Update(byte[] buf);
60
61 /*
62 * Add some bytes to the current input ('len' bytes from buf[],
63 * starting at offset 'off').
64 */
65 void Update(byte[] buf, int off, int len);
66
67 /*
68 * Finalize the hash computation and write the output in the
69 * provided outBuf[] array (starting at offset 'off'). This
70 * instance is also automatically reset (as if by a Reset() call).
71 */
72 void DoFinal(byte[] outBuf, int off);
73
74 /*
75 * Finalize the hash computation and write the output into a
76 * newly allocated buffer, which is returned. This instance
77 * is also automatically reset (as if by a Reset() call).
78 */
79 byte[] DoFinal();
80
81 /*
82 * Finalize the current hash computation but keep it active;
83 * this thus returns the hash value computed over the input
84 * bytes injected so far, but new bytes may be added afterwards.
85 * The output is written in outBuf[] at offset 'off'.
86 */
87 void DoPartial(byte[] outBuf, int off);
88
89 /*
90 * Finalize the current hash computation but keep it active;
91 * this thus returns the hash value computed over the input
92 * bytes injected so far, but new bytes may be added afterwards.
93 * The output is written into a newly allocated array, which
94 * is returned.
95 */
96 byte[] DoPartial();
97
98 /*
99 * Encode the current running state into the provided buffer.
100 * This is defined for functions that employ an internal padding
101 * but no special finalization step (e.g. MD5, SHA-1, SHA-256);
102 * the running state is the one resulting from the last
103 * processed block.
104 *
105 * Note: for SHA-224, SHA-384 and similar functions, the current
106 * state as returned by this method will be truncated to the
107 * actual hash output length.
108 */
109 void CurrentState(byte[] outBuf, int off);
110
111 /*
112 * Reset the internal state, to start a new computation. This
113 * can be called at any time.
114 */
115 void Reset();
116
117 /*
118 * Duplicate this engine. This creates a new, independent
119 * instance that implements the same function, and starts with
120 * the current state.
121 */
122 IDigest Dup();
123
124 /*
125 * Compute the hash of a given input in one call; this does NOT
126 * change the internal state.
127 */
128 byte[] Hash(byte[] buf);
129
130 /*
131 * Compute the hash of a given input in one call; this does NOT
132 * change the internal state.
133 */
134 byte[] Hash(byte[] buf, int off, int len);
135 }
136
137 }