Java MessageDigest code equivalent to C#

  • Thread starter Thread starter Tech Aspirant
  • Start date Start date
T

Tech Aspirant

Guest
Hello

I want to convert the below Java code to C#. Below is the code in C# which I tried but for md5.update I don't know whats equivalent to the same.

JAVA Code

private byte[] encodeChapPassword(String plaintext, byte[] chapChallenge) {
// see RFC 2865 section 2.2
byte chapIdentifier = (byte) random.nextInt(256);
byte[] chapPassword = new byte[17];
chapPassword[0] = chapIdentifier;

MessageDigest md5 = getMd5Digest();
md5.reset();
md5.update(chapIdentifier);
md5.update(RadiusUtil.getUtf8Bytes(plaintext));
byte[] chapHash = md5.digest(chapChallenge);

System.arraycopy(chapHash, 0, chapPassword, 1, 16);
return chapPassword;
}

C# Code

private byte[] encodeChapPassword(String plaintext, byte[] chapChallenge)
{
Random random = new Random();
byte chapIdentifier = ((byte)(random.Next(256))); //byte chapIdentifier = (byte)random.nextInt(256);
byte[] chapPassword = new byte[17]; //byte[] chapPassword = new byte[17];
chapPassword[0] = chapIdentifier; //chapPassword[0] = chapIdentifier;
MD5 md5 = MD5.Create(); //MessageDigest md5 = getMd5Digest();
md5.Clear(); //md5.reset();

byte[] chapHash = md5.ComputeHash(chapChallenge); //byte[] chapHash = md5.digest(chapChallenge);

Array.Copy(chapHash, 0, chapPassword, 1, 16); //System.arraycopy(chapHash, 0, chapPassword, 1, 16);
return chapPassword; //return chapPassword;
}
I don't know what will be the C# equivalent to md5.update(); and also let me know if anything wrong in my existing c# code.

Continue reading...
 
Back
Top