sobota, 11 maja 2013

JAVA vs .NET differences in practice

Hello long time I didn't write any post - it's time to change it! Today I would like to show you in practice some differences between .NET and Java. I mean in some primitives data types and other. Few days ago I was involved to some task with hashed users passwords. It was necessary for test Web Services (JAX-WS) whose connect with some other services using hashed password. We have some hash algorithm which was written in .NET and we would like to have the same result in Java but it wasn't working. OK let's have some details. This is some similar example in .NET
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.     private string generateSuperHash()
  2.  
  3.         {
  4.             byte[] t1= { 0x1f, 0xcd, 0x5, 0x12, 0xf3, 0xaa };
  5.      // byte[] t1= { 31, 205, 5, 18, 243, 170};
  6.  
  7.             byte[] t2= { 0xf3, 0x3d, 0x1, 0xb1, 0x3d, 0xc5 };
  8.     //  byte[] t2= { 243, 61, 1, 177, 61, 197};
  9.             byte[] t3= new byte[6];
  10.  
  11.             for (int i= 0; i< t3.GetLength(0); i++)
  12.             {
  13.                 t3[i] = Convert.ToByte((Convert.ToUInt16(t1[i]) ^ Convert.ToUInt16(t2[i])));
  14.             }
  15.             return Convert.ToBase64String(t3);
  16.         }



From .NET point of view everything is fine, but from Java is not! Why? - In .NET byte range is between 0-255 in Java is between (-127,128). This is 1 fail which I have discovered in code. Solution? - change the type from byte to int in Java and 1 problem fixed up.(But not really...). OK in Java there is a bitwise operator ^ (XOR) so - Convert.ToByte in Java is simple.
But XOR wouldn't work on int type, if I cast int to byte i will lost precision. Damn! There is another valuable information and comparison -Convert.ToBase64String and SHA512 algorithm. What is a problem with SHA512 algorithm in Java? There is to many code (in pure JDK) to write, but if I add just 1 library/jar Apache Commons Codec my code reduces from around 20 lines of code just to 1.
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.     private String hashSHA512(String param)
  2.     {
  3.         return DigestUtils.sha512Hex(param);
  4.     }
But what about ToBase64String? I was searching in google and there was to many code to write the equivalent. In .NET the programmer sent to the method ToBase64String a whole table with bytes, in Java even when I tried get all bytes and "stick" them together using StringBuffer/StringBuilder or many various combinations and approaches and send it to equivalent Base64 method from Apache Commons Codec but without good effects. Time was running up, and I feel some solution in the air, just like a wolf which felt the blood. I didn't give up, what I did is just use online .NET/C# compilator and write the code and run it!

So my final solution in Java was really short and works well.
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package pl.test;
  2.  
  3. import org.apache.commons.codec.digest.DigestUtils;
  4.  
  5. public class HashPassword {
  6.  
  7.     public static void main(String[] args) {
  8.         HashPassword hp = new HashPassword();
  9.  
  10.         String hash = hp.hashToLDAP(hp.getUserID(), hp.getUserPass(), superHash);
  11.         System.out.println("Hash ---> " + hash);
  12.     }
  13.  
  14.     private final static String superHash = "PAEo85v";
  15.     private String userID = "1234567";
  16.     private String userPass = "01234569ASP";
  17.     private String hashToLDAP(String clientID, String clientPass, String superHash) {
  18.         String finalHash = hashSHA512((hashSHA512(clientPass + getSuperhash())) + clientID);
  19.         return finalHash;
  20.     }
  21.     private static String hashSHA512(String param) {
  22.         return DigestUtils.sha512Hex(param);
  23.     }
  24.     public String getUserID() {
  25.         return userID;
  26.     }
  27.     public static String getSuperhash() {
  28.         return superHash;
  29.     }
  30.     public String getUserPass() {
  31.         return userPass;
  32.     }
  33. }
After this I gained a lot in my new Team mates eyes. But they don't know that what kind a "lazy" bustard I am ;)

What is conclusion ? -Sometimes we are wasting time to find solution using another libraries, searching equivalent type in Java/.NET and a solution is just at your fingertips. Sometimes it's better to run any online compiler and run the code and see the result, save the output and use it in our code without wasting time to translate byte> 128 to int and many miracles in Java/.NET

Brak komentarzy:

Prześlij komentarz