aboutsummaryrefslogtreecommitdiff
path: root/Emby.Common.Implementations/IO/SharpCifs/Util/HMACT64.cs
blob: 9ab31d61b80ed4da015521ff44721cfa3779135e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// This code is derived from jcifs smb client library <jcifs at samba dot org>
// Ported by J. Arturo <webmaster at komodosoft dot net>
//  
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
using System;
using SharpCifs.Util.Sharpen;

namespace SharpCifs.Util
{
	/// <summary>This is an implementation of the HMACT64 keyed hashing algorithm.</summary>
	/// <remarks>
	/// This is an implementation of the HMACT64 keyed hashing algorithm.
	/// HMACT64 is defined by Luke Leighton as a modified HMAC-MD5 (RFC 2104)
	/// in which the key is truncated at 64 bytes (rather than being hashed
	/// via MD5).
	/// </remarks>
	public class Hmact64 : MessageDigest
	{
		private const int BlockLength = 64;

		private const byte Ipad = unchecked(unchecked(0x36));

		private const byte Opad = unchecked(unchecked(0x5c));

		private MessageDigest _md5;

		private byte[] _ipad = new byte[BlockLength];

		private byte[] _opad = new byte[BlockLength];

		/// <summary>Creates an HMACT64 instance which uses the given secret key material.</summary>
		/// <remarks>Creates an HMACT64 instance which uses the given secret key material.</remarks>
		/// <param name="key">The key material to use in hashing.</param>
		public Hmact64(byte[] key) 
		{
			int length = Math.Min(key.Length, BlockLength);
			for (int i = 0; i < length; i++)
			{
				_ipad[i] = unchecked((byte)(key[i] ^ Ipad));
				_opad[i] = unchecked((byte)(key[i] ^ Opad));
			}
			for (int i1 = length; i1 < BlockLength; i1++)
			{
				_ipad[i1] = Ipad;
				_opad[i1] = Opad;
			}
			try
			{
				_md5 = GetInstance("MD5");
			}
			catch (Exception ex)
			{
				throw new InvalidOperationException(ex.Message);
			}
			EngineReset();
		}


		protected byte[] EngineDigest()
		{
			byte[] digest = _md5.Digest();
			_md5.Update(_opad);
			return _md5.Digest(digest);
		}

		protected int EngineDigest(byte[] buf, int offset, int len)
		{
			byte[] digest = _md5.Digest();
			_md5.Update(_opad);
			_md5.Update(digest);
			try
			{
				_md5.Digest(buf, offset, len);

			    return len;
			}
			catch (Exception)
			{
				throw new InvalidOperationException();
			}
		}

		protected int EngineGetDigestLength()
		{
			return _md5.GetDigestLength();
		}

		protected void EngineReset()
		{
			_md5.Reset();
			_md5.Update(_ipad);
		}

		protected void EngineUpdate(byte b)
		{
			_md5.Update(b);
		}

		protected void EngineUpdate(byte[] input, int offset, int len)
		{
			_md5.Update(input, offset, len);
		}

        public override byte[] Digest()
        {
            return EngineDigest();
        }

        public override int GetDigestLength()
        {
            return EngineGetDigestLength();
        }

        public override void Reset()
        {
            EngineReset();
        }

        public override void Update(byte[] b)
        {
            EngineUpdate(b, 0, b.Length);
        }

        public override void Update(byte b)
        {
            EngineUpdate(b);
        }

        public override void Update(byte[] b, int offset, int len)
        {
            EngineUpdate(b, offset, len);
        }
    }
}