Вопрос

Looking for help converting this C# code to Java

I have the byte array but need help coding the conversion to BigInteger. Looking to create a UDF for Hadoop;

C# Code Ouput, this is the same as SQL Server

//A4-B7-83-01-00-59-25-A8-B5-7C-F7-16-E6-69-CF-14-A2-2E-22-09
//-6330555844639082588

// Unicode
byte[] byteArray = Encoding.UTF8.GetBytes("JAO21V279RSNHYGX23L0");
//Console.WriteLine(byteArray);
System.Security.Cryptography.SHA1 sha = new System.Security.Cryptography.SHA1CryptoServiceProvider();
byte[] hashedPasswordBytes = sha.ComputeHash(byteArray);
//Console.WriteLine(hashedPasswordBytes);
Console.WriteLine("1: " + BitConverter.ToString(hashedPasswordBytes));
//Console.WriteLine(BitConverter.ToInt64(hashedPasswordBytes,0));
long value = BitConverter.ToInt64(hashedPasswordBytes, 0);
Console.WriteLine("2: " + value);
if (BitConverter.IsLittleEndian)
    Array.Reverse(hashedPasswordBytes);
Console.WriteLine("3: " + BitConverter.ToString(hashedPasswordBytes));
value = BitConverter.ToInt64(hashedPasswordBytes, 0);
Console.WriteLine("4: " + value);

1: A4-B7-83-01-00-59-25-A8-B5-7C-F7-16-E6-69-CF-14-A2-2E-22-09 2: -6330555844639082588 Flipped Values 3: 09-22-2E-A2-14-CF-69-E6-16-F7-7C-B5-A8-25-59-00-01-83-B7-A4 4: -1843714884904279543 -- Correct BigInt

SQL Server code

DECLARE @InputString VARCHAR(MAX) = 'JAO21V279RSNHYGX23L0'

SELECT CONVERT(BIGINT, HashBytes('SHA1', @InputString)) , HashBytes('SHA1', @InputString)

-1843714884904279543 0xA4B78301005925A8B57CF716E669CF14A22E2209

Java Output strHash: 1a41b71831011001591251a81b517c1f71161e61691cf1141a212e122109

Java Code that I have so far import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.lang.System.*; import static java.lang.System.out; import java.math.BigInteger; import java.nio.charset.Charset;

class JceSha1Test {

  private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");

  public static void main(String[] a) {
      try {
        String strHash = "JAO21V279RSNHYGX23L0";        
        strHash = encryptPassword(strHash);        
        System.out.println("strHash: " + strHash);
      } catch (Exception e) {
         System.out.println("Exception: "+e);
      }
   }
  public static String encryptPassword(String password) {
    String returnValue = null;
    byte[] buf = password.getBytes(UTF8_CHARSET);

    ByteBuffer bb = ByteBuffer.wrap( buf );
    MessageDigest algorithm=null;
    try {
      algorithm = MessageDigest.getInstance("SHA-1");

    } catch (NoSuchAlgorithmException e) {
      //sClassLog.logException(e);
    }
    algorithm.reset();
    algorithm.update(buf);
    byte[] digest = algorithm.digest();
    returnValue = "";

    for (int byteIdx = 0; byteIdx < digest.length; byteIdx++) {
      //System.out.println( Integer.toHexString(digest[byteIdx]) );
      //returnValue += Integer.toHexString(digest[byteIdx] + 256  & 0xFF);
      //returnValue += Integer.toHexString((digest[byteIdx] + 256   & 0xFF) + 0x100 );
      //returnValue += Integer.toHexString( ( digest[byteIdx] & 255 ) );
      //returnValue += Integer.toHexString( ( 0xFF & digest[byteIdx] ) );
      //returnValue += Integer.toHexString( ( digest[byteIdx] & 0xFF ) );      
      returnValue += Integer.toString( ( digest[byteIdx] & 0xFF ) + 0x100, 16).substring( 1 );      
      //returnValue += Integer.toHexString( ( digest[byteIdx] + 256 ) ); // Orig
    }
    return returnValue;    
  }
//A4-B7-83-01-00-59-25-A8-B5-7C-F7-16-E6-69-CF-14-A2-2E-22-09
//-1843714884904279543
}

Thanks for any help!

///////////////////////////**************************////////////////////////

My final code this matches the HashBytes in SQL Server:

package com.cb.hiveudf;

import java.nio.ByteBuffer; 
import java.nio.ByteOrder; 
import java.security.MessageDigest; 
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.LongWritable;


/**
 * Implementation of the HashBytes UDF found in many databases.
 */

@Description(name = "hashBytes", 
    value = "_FUNC_(Charset, Value) - assigns a unique Biginterger to each string to which it is applied",
    extended = "Example:\n "
    + "  > SELECT name, _FUNC_(\"UTF-16LE\", value) hashkey FROM src" + "  ")

public class UDFHashBytes extends UDF {
 private final LongWritable result = new LongWritable(); 

    public LongWritable evaluate(Text input) throws Exception {
        if (input == null) 
        {
              return null;
        } 
        else 
        {
            String hashstring = input.toString();
            byte[] buf = hashstring.getBytes("UTF-8");
            MessageDigest algorithm=null;
            algorithm = MessageDigest.getInstance("SHA-1");          
            algorithm.reset();
            algorithm.update(buf);
            byte[] digest = algorithm.digest();  
            if(java.nio.ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
              for (int i = 0, j = digest.length - 1; i < j; i++, j--)  
              {  
                byte b = digest[i];  
                digest[i] = digest[j];  
                digest[j] = b;  
              } 
            }    
            ByteBuffer bb = ByteBuffer.wrap( digest );
            if(java.nio.ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) 
            {
              bb.order(ByteOrder.LITTLE_ENDIAN);
            }    

            result.set(bb.getLong());
            return result;
        }
    }
}
Это было полезно?

Решение

Well, I hope you can still recognize some of the code :)

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

final class JceSha1Test {
    public static void main(final String ... args) {
        final String strHashInput = "JAO21V279RSNHYGX23L0";
        final byte[] strHash = hashPassword(strHashInput);
        System.out.println("strHash: " + toHex(strHash));
        final ByteBuffer strHashBuffer = ByteBuffer.wrap(strHash);
        strHashBuffer.order(ByteOrder.LITTLE_ENDIAN);
        final long test = strHashBuffer.getLong();
        System.out.println(test);
    }

    public static byte[] hashPassword(final String password) {
        final byte[] encodedPassword = password.getBytes(StandardCharsets.UTF_8);
        final MessageDigest algorithm;
        try {
            algorithm = MessageDigest.getInstance("SHA-1");
        } catch (final NoSuchAlgorithmException e) {
            throw new IllegalStateException(e);
        }
        return algorithm.digest(encodedPassword);
    }

    public static String toHex(final byte[] data) {
        final StringBuilder sb = new StringBuilder(data.length * 2);
        for (int i = 0; i < data.length; i++) {
            sb.append(String.format("%02X", data[i]));
        }
        return sb.toString();
    }
}

Другие советы

THe SQL uses bigEndin but C# (and other languages) uses Little Endian for GetBytes. Thus, reverse the result after GetBytes.

 static long ComputeSha256HashbigInt(string rawData)
    {
        // Create a SHA256   
        using (SHA256 sha256Hash = SHA256.Create())
        {
            // ComputeHash - returns byte array  
            byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));

            if (BitConverter.IsLittleEndian) Array.Reverse(bytes);

            long value = BitConverter.ToInt64(bytes, 0);

            return value;
        }
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top