Posted September 2, 20168 yr Hello Friends, I'm trying to implement one key generation logic developed in java in to c#. So can anyone tell me how to convert it in c# so that both the java and C# programs will work in similar manner. My java method which will implement the key is as below. public static String calculateOTP(byte[] seedValue, String userPIN, String challenge) { try { SHA1 sha1 = new SHA1(); byte[] hashedChallenge = sha1.digest(challenge.getBytes()); sha1.init(); sha1.update(seedValue); sha1.update(hashedChallenge); sha1.finish(); byte[] otp0byte = sha1.digest(); int otp0 = 0; for (int i = 17; i < otp0byte.length; i++) { otp0 = (otp0 << 8) | (otp0byte[i] & SecretKeyPacket.USAGE_CHECKSUM); } String num = Integer.toString((otp0 + Integer.parseInt(userPIN)) & ViewCompat.MEASURED_SIZE_MASK, 16); while (num.length() < 6) { num = "0" + num; } return num; } catch (Exception e) { return null; } } If you know how to effectively do this than please help me. Edited September 2, 20168 yr by kate
September 2, 20168 yr 9 minutes ago, kate said: otp0 = (otp0 << 8) | (otp0byte & SecretKeyPacket.USAGE_CHECKSUM); This code makes no sense to me. otp0byte is an array, SecretKeyPacket.USAGE_CHECKSUM seems to be integer constant. Maybe Java allows you to do that, but I have no idea what the result would be. Apart from that, C# code will look almost the same.
September 2, 20168 yr 9 minutes ago, kao said: This code makes no sense to me. otp0byte is an array, SecretKeyPacket.USAGE_CHECKSUM seems to be integer constant. Maybe Java allows you to do that, but I have no idea what the result would be. Apart from that, C# code will look almost the same. Probable will not even compile! probable the good code is this: otp0 = (otp0 << 8) | (otp0byte [ i ] & SecretKeyPacket.USAGE_CHECKSUM); Crap it is board problem: can't enter " [ i ] " without spaces " " ! Edited September 2, 20168 yr by CodeCracker
September 2, 20168 yr Author Sorry guys my mistake. I must have to use code tags. Now I have modified the post. Please take a look in to it now.
September 3, 20168 yr Please don't PM me. I will respond in public threads if and when I have something to say. You can use SHA1Managed class for hash calculation: TransformBlock function replaces sha1.update, TransformFinalBlock replaces sha1.finish. int.Parse replaces Integer.parseInt and String.Format will format the final result. That's it.
Create an account or sign in to comment