2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
30 * This interface qualifies a hash function implementation.
33 public interface IDigest {
36 * Get the hash function symbolic name.
41 * Get the hash function output size, in bytes.
43 int DigestSize { get; }
46 * Get the hash function block size, in bytes (the block
47 * size is used in the definition of HMAC).
49 int BlockSize { get; }
52 * Add one byte to the current input.
57 * Add some bytes to the current input.
59 void Update(byte[] buf);
62 * Add some bytes to the current input ('len' bytes from buf[],
63 * starting at offset 'off').
65 void Update(byte[] buf, int off, int len);
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).
72 void DoFinal(byte[] outBuf, int off);
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).
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'.
87 void DoPartial(byte[] outBuf, int off);
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
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
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.
109 void CurrentState(byte[] outBuf, int off);
112 * Reset the internal state, to start a new computation. This
113 * can be called at any time.
118 * Duplicate this engine. This creates a new, independent
119 * instance that implements the same function, and starts with
125 * Compute the hash of a given input in one call; this does NOT
126 * change the internal state.
128 byte[] Hash(byte[] buf);
131 * Compute the hash of a given input in one call; this does NOT
132 * change the internal state.
134 byte[] Hash(byte[] buf, int off, int len);