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 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; }
36 * void Update(byte[] buf, int off, int len)
37 * void DoPartial(byte[] outBuf, int off)
41 * Implementations SHOULD provide overrides for:
42 * void CurrentState(byte[], int)
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()
51 public abstract class DigestCore : IDigest {
54 public abstract string Name { get; }
57 public abstract int DigestSize { get; }
60 public abstract int BlockSize { get; }
63 public abstract void Update(byte b);
66 public virtual void Update(byte[] buf)
68 Update(buf, 0, buf.Length);
72 public abstract void Update(byte[] buf, int off, int len);
75 public abstract void DoPartial(byte[] outBuf, int off);
78 public virtual byte[] DoPartial()
80 byte[] buf = new byte[DigestSize];
86 public virtual void DoFinal(byte[] outBuf, int off)
88 DoPartial(outBuf, off);
93 public virtual byte[] DoFinal()
95 byte[] r = DoPartial();
101 public abstract void Reset();
104 public abstract IDigest Dup();
107 * Default implementation throws a NotSupportedException.
109 public virtual void CurrentState(byte[] outBuf, int off)
111 throw new NotSupportedException();
115 public virtual byte[] Hash(byte[] buf)
117 return Hash(buf, 0, buf.Length);
121 public virtual byte[] Hash(byte[] buf, int off, int len)
125 h.Update(buf, off, len);