kate Posted September 2, 2016 Posted September 2, 2016 (edited) 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, 2016 by kate
kao Posted September 2, 2016 Posted September 2, 2016 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.
CodeExplorer Posted September 2, 2016 Posted September 2, 2016 (edited) 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, 2016 by CodeCracker
kao Posted September 2, 2016 Posted September 2, 2016 That's why people should learn to use "code" tags! :@
kate Posted September 2, 2016 Author Posted September 2, 2016 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.
kao Posted September 3, 2016 Posted September 3, 2016 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.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now