寻找将此C#代码转换为Java的帮助
我有字节数组,但需要帮助编码到BigInteger的转换。希望为Hadoop创建一个UDF;
//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);
DECLARE @InputString VARCHAR(MAX) = 'JAO21V279RSNHYGX23L0'
SELECT CONVERT(BIGINT, HashBytes('SHA1', @InputString)) , HashBytes('SHA1', @InputString)
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
}
//////////////////////////////
我的最后一段代码与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;
}
}
}
好吧,我希望你还能认出一些代码:)
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();
}
}
我对将纯java Curve25519函数转换为Python等效函数存在问题,具体问题与将哈希字符串转换为字节等效函数的摘要函数有关,java实现: 数据示例: sP=“这是一个用于测试目的的密码短语示例” 生成此字节输出: 82, -57, 124, 58, -105, 76, 123, 3, 119, -21, 121, 71, -54, 73, -75, 54, 31, -33, -49,
问题内容: 我有一个实现了hashCode()的向量类。它不是我写的,而是使用2个质数对2个向量分量进行异或运算。这里是: …因为这是来自已建立的Java库,所以我知道它可以正常工作。 然后,我有一个Boundary类,其中包含2个向量:“开始”和“结束”(代表直线的端点)。这两个向量的值是边界的特征。 在这里,我尝试为构成该边界的向量的唯一2元组(起点和终点)创建一个良好的hashCode()。
问题内容: 我对加密/哈希知之甚少。 我必须对加密密钥进行哈希处理。Java中的示例是这样的… 现在,如果我错了,请纠正我,但是上面的代码使用MD5算法对字符串进行了哈希处理。 当我在C#中哈希相同的字符串时,我希望得到相同的结果。 我当前的C#代码看起来像这样… 但是末字节结果不匹配。 Java得到… C#得到… 我需要C#代码才能获得与Java代码相同的结果(不是相反),有什么想法吗? 谢谢。
问题内容: 我有一个持有以下值的JSON对象: 我想在Ruby中循环遍历以获得键/值对。当我使用时,它不会遍历对象,因为它不是Ruby哈希形式: 如何将上述JSON对象转换为Ruby哈希? 问题答案: 接下来的代码片段呢?
主要内容:Hashtable 类中的属性,Hashtable 类中的方法在 C# 中,Hashtable(哈希表) 类表示根据键的哈希代码进行组织的键(key)/值(value)对的集合,可以使用键来访问集合中的元素。也就是说当您需要使用键来访问指定元素时,可以选择使用哈希表。 Hashtable 类中的属性 下表中列出了 Hashtable 类中一些常用的属性: 属性 描述 Count 获取哈希表中包含的键值对的个数 IsFixedSize 获取一个值,用来表示哈希
Hashmaps通常使用桶的内部数组(表)来实现。在通过键访问hashmap时,我们使用键类型特定(逻辑类型特定)的hash函数获得键的hashcode。然后我们需要将hashcode映射到实际的内部桶表索引。 有时,内部表可能会收缩和扩展,这取决于hashmap填充率。那么可能是散列码- 例如,我们的哈希函数返回32位无符号整数值 时刻A:内表容量为10000 时刻B:内工作台容量为100000