Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have Java code to convert into C#. Below is the code:

Java:

Java
final MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.reset();
            digest.update(secretKey.getBytes("UTF-8"));
            
            final String key = new String(Hex.encodeHex(digest.digest())).substring(0, 32);


You can find the Hex.encodeHex from here.


My C# Code (incomplete) :

C#
var sha = new SHA1CryptoServiceProvider();
                string b64 = ByteArrayToString(Encoding.UTF8.GetBytes(secretKey));
                var b64Bytes = Encoding.UTF8.GetBytes(b64);



I think below two lines should be added in my C# code.

C#
var result = sha.ComputeHash(b64Bytes);
               var finalResult = BitConverter.ToString(result).Replace("-", "").ToLower();



For more information here, secretKey is a 16 letters long hexadecimal value. I need it's 32 bytes.

Thanks
Kapil
Posted
Comments
Andy Lanng 25-Mar-15 10:49am    
What is the question?
Kapil Waghe 25-Mar-15 13:28pm    
How to convert above java code into c#. I shared what I have tried till now.
Rolyvan Ezra Mellolo 19-Dec-17 1:11am    
public String buildPasswordDigest(String nonce, String created, String appSecret)
{return base_64(sha_256(nonce, created, appSecret));}

public byte[] sha_256 (String nonce, String created, String appsecret)
{try{MessageDigest sha = MessageDigest.getInstance("SHA-256"); return sha.digest((nonce+created+appsecret).getBytes());}catch (NoSuchAlgorithmException e){throw new RuntimeException(e);}

public String base_64(byte[] bytes){return new String(org.apache.commons.codec.binary.Base64.encodeBase64(bytes));}

convert to C#

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900