Hmm, es geht um die HMAC...es ist ehrlich gesagt ziemlich hirnrissig MD5 als Hexstring zurückzugeben und das wieder in Bytes umzuwandeln für die HMAC Berechnung. Das kannst du alles richtig auf binärer Ebene machen. Falls du etwas damit anfangen kannst hier ist eine HMACMD5 Klasse in C#, die ich geschrieben habe.
using System;
using System.Security.Cryptography;
using System.Diagnostics;
using DSNTS.BaseServices;
namespace DSNTS.EncryptionServices
{
/// <summary>
/// Summary description for HMACMD5.
/// </summary>
[CodeCategory("ES")]
public sealed class HMACMD5 : KeyedHashAlgorithm
{
private const int md5BlockLength = 64;
private HashAlgorithm _md5 = new MD5CryptoServiceProvider();
private byte[] _key = null;
private byte[] _kxipad = null;
private byte[] _kxopad = null;
private byte[] _td = null;
private bool _initDone = false;
public HMACMD5() : base()
{
init();
}
public HMACMD5(byte[] key) : base()
{
init();
this.Key = key;
}
private void init()
{
_td = new byte[md5BlockLength];
_kxipad = new byte[md5BlockLength];
_kxopad = new byte[md5BlockLength];
}
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
checkDisposed();
if (!_initDone) Initialize();
while(cbSize>0)
{
int size = cbSize<=_td.Length?cbSize:_td.Length;
_md5.TransformBlock(array, ibStart, size, _td, 0);
cbSize -= size;
ibStart += size;
}
}
protected override byte[] HashFinal()
{
checkDisposed();
_md5.TransformFinalBlock(_td, 0, 0);
byte[] hv = _md5.Hash;
_md5.Initialize();
_md5.TransformBlock(_kxopad, 0, _kxopad.Length, _td, 0);
_md5.TransformFinalBlock(hv, 0, hv.Length);
_initDone = false;
return _md5.Hash;
}
public override void Initialize()
{
checkDisposed();
if (_key==null)
{
// Generate random key if none set
RandomNumberGenerator rng = RandomNumberGenerator.Create();
_key = new byte[md5BlockLength];
rng.GetBytes(_key);
initKey();
}
_md5.Initialize();
_md5.TransformBlock(_kxipad, 0, _kxipad.Length, _td, 0);
_initDone = true;
}
public override int HashSize
{
get
{
return 16*8;
}
}
public override byte[] Key
{
get
{
return _key;
}
set
{
checkDisposed();
if (State!=0)
throw new CryptographicException("Hash key cannot be changed after the" & _
"first write to the stream");
_key = value;
initKey();
Initialize();
}
}
private void initKey()
{
// Do the stuff to set up the new key
KeyValue = _key;
if (KeyValue.Length>md5BlockLength)
{
_md5.Initialize();
KeyValue = _md5.ComputeHash(KeyValue, 0, KeyValue.Length);
}
for (int i=0; i<KeyValue.Length; i++) _kxipad[ i ] = KeyValue[ i ];
for (int i=0; i<md5BlockLength; i++) _kxipad[ i ] ^= 0x36;
for (int i=0; i<KeyValue.Length; i++) _kxopad[ i ] = KeyValue[ i ];
for (int i=0; i<md5BlockLength; i++) _kxopad[ i ] ^= 0x5C;
}
public new static KeyedHashAlgorithm Create()
{
return new HMACMD5();
}
protected override void Dispose(bool disposing)
{
if (_md5!=null)
_md5.Clear();
_md5 = null;
}
private void checkDisposed()
{
if (_md5==null) throw new ObjectDisposedException(this.ToString());
}
} // class HMACMD5
}Das funktioniert wie die anderen .Net KeyedHashAlgorithm Klassen.
________
Alle Angaben ohne Gewähr. Keine Haftung für Vorschläge, Tipps oder sonstige Hilfe, falls es schiefgeht, nur Zeit verschwendet oder man sonst nicht zufrieden ist |