Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

how to convert Java method in to C#

Featured Replies

Posted

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 by kate

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.

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 by CodeCracker

That's why people should learn to use "code" tags! :@

  • 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.

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.