这是我的PHP代码:
hash_hmac( "sha256", utf8_encode( $filename ), utf8_encode( $password ) );
Run Code Online (Sandbox Code Playgroud)
这是我的C#代码:
var hmacsha256 = new HMACSHA256( Encoding.UTF8.GetBytes( password ) );
hmacsha256.ComputeHash( Encoding.UTF8.GetBytes( filename ) );
Run Code Online (Sandbox Code Playgroud)
不幸的是,两个结果都不同 任何人都可以给我一个提示吗?
有时我需要通过组合其几个实例成员的hashCodes来实现obj的hashCode()方法.例如,如果组合obj有成员a,b和c,我经常看到ppl将其实现为
int hashCode(){
return 31 * 31 * a.hashCode() + 31 * b.hashCode() + c.hashCode();
}
Run Code Online (Sandbox Code Playgroud)
这个神奇的数字31来自哪里?它是4字节的长度还是只是素数?
有没有其他优选/标准的方法来实现hashCode()?
我有两个字符串,id喜欢使用它们作为字典键,但我有点懒惰创建另一个对象,计算字符串的哈希码等.
所以不是那样,我可以获得两个字符串的哈希码,添加它们并使用结果作为字典的键吗?
有可能引起碰撞吗?对?.
有任何想法吗?
我有道德困境.我的应用程序中有一些值对象,它们是不可变的,非常简单.我用IDE生成了equals和hashcode(在我的例子中是intellij),但这样做,使代码覆盖率下降,加上报告现在表明那些值对象非常复杂(使用圈复杂度量度量)实际上他们很简单.
例如,以下等于在具有3个不可变属性的值对象中.代码复杂度为14(javaNCSS),它有26个执行分支(Cobertura).我还应该补充一点,如果任何方法的复杂度大于10,我就会失败.
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TranscriptTaskDetails that = (TranscriptTaskDetails) o;
if (inputFile != null ? !inputFile.equals(that.inputFile) : that.inputFile != null) {
return false;
}
if (language != that.language) {
return false;
}
if (outputFile != null ? !outputFile.equals(that.outputFile) : that.outputFile != null) {
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
我想知道其他开发人员使用什么来规避这一点,因为我非常关注复杂性报告,因为根据我的经验,高复杂度指标涉及更多错误,所以这个自动生成的equals和hashcode正在污染报告.
我正在考虑使用来自apache commons-lang的EqualsBuilder和HashcodeBuilder来规避这个问题,但我并不百分之百:S.
我应该补充一点,我为这个项目编写的代码部分是一个将被其他业务部门使用的库......并且将由不同的团队维护:S.
由于==不能使用Arrays,我无法有效地创建一组数组(或使用数组键映射).我宁愿不把我的数组转换为Vector或List或其他东西.有没有一种轻量级的方法来定义Arrays上的自然比较和哈希码,所以我可以将它们粘贴在一个Set中?
我想知道这是什么意思时的javadoc的TreeSet说
这个类实现了由TreeMap实例支持的Set接口?
在下面的示例中,我还没有实现该Hashcode方法,但仍按预期工作,即它能够对对象进行排序.请注意,我故意没有实现一致的Equals实现来检查TreeSet行为.
import java.util.TreeSet;
public class ComparisonLogic implements Comparable<ComparisonLogic>{
String field1;
String field2;
public String toString(){
return field1+" "+field2;
}
ComparisonLogic(String field1,String field2){
this.field1= field1;
this.field2= field2;
}
public boolean equal(Object arg0){
ComparisonLogic obj = (ComparisonLogic) arg0;
if(this.field1.equals(obj.field1))
return true;
else
return false;
}
public int compareTo(ComparisonLogic arg0){
ComparisonLogic obj = (ComparisonLogic) arg0;
return this.field2.compareToIgnoreCase(obj.field2);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO …Run Code Online (Sandbox Code Playgroud) 内置的HASH()函数使用什么样的哈希算法?
我理想地寻找SHA512/SHA256哈希,类似于SHA()函数在猪的linkedin datafu UDF中提供的哈希.
我遇到了以下问题.我想使用java.util.HashMap,并java.util.PriorityQueue在犀牛脚本,在这里我需要使用特定的自定义对象为在HashMap的关键,并且还使用HashMap.containsKey()以检查是否有在Map中的键(另一种选择是检查的对象Collection.contains(Object o)).
所以,显然,我需要在我的对象中基于一些字段值实现equals和hashCode.
例如:
试图使用JavaScript.由于JavaScript没有那些方法不起作用.请参阅样品1和样品2
扩展java.lang.Object.样本3.部分工作,正在调用方法.但
在Java中实现我的自定义类并在JavaScript中扩展它.样本4.作品.但如果我必须使用Java,我是否需要Nashorn?
var PriorityQueue = java.util.PriorityQueue;
var HashMap = java.util.HashMap;
var Integer = java.lang.Integer;
// Sample 1
// Doesn't work, equals and hashCode are not being invoked
function Vertex1(from, cost) {
this.from = from;
this.cost = cost;
this.equals = function(other) { return this.from == other.from; }
this.hashCode = function() { return Integer.hashCode(this.from); } …Run Code Online (Sandbox Code Playgroud) hashCode()HotSpot 的默认实现返回一个随机值并将其存储在对象头中.这在Java 8中似乎没有改变,其中哈希值是通过调用来计算的os::random():
static inline intptr_t get_next_hash(Thread * Self, oop obj) {
intptr_t value = 0 ;
if (hashCode == 0) {
// This form uses an unguarded global Park-Miller RNG,
// so it's possible for two threads to race and generate the same RNG.
// On MP system we'll have lots of RW access to a global, so the
// mechanism induces lots of coherency traffic.
value = os::random() ;
} else …Run Code Online (Sandbox Code Playgroud) 我想为字符串生成一个整数哈希码,它将永远保持不变; 即相同的字符串应始终产生相同的哈希码.
散列不必是加密安全的,它不会用于密码或敏感数据.
我的第一次尝试是使用.net框架string.GetHashCode()函数.但是在阅读消息来源后,我发现了以下内容:
Run Code Online (Sandbox Code Playgroud)// We want to ensure we can change our hash function daily. // This is perfectly fine as long as you don't persist the // value from GetHashCode to disk or count on String A // hashing before string B. Those are bugs in your code. hash1 ^= ThisAssembly.DailyBuildNumber;
这似乎表明哈希码不会保持不变.
如果是这样,框架是否有另一种方法来生成可重复的哈希码?或者GetHashCode的代码是否是实现我自己的代码的合理起点?
我正在寻找尽可能轻巧和快速的东西.
我找到了System.Security.Cryptography.MD5,但对于一个简单的int32哈希码来说这似乎有点过头了,我担心开销.至少它需要从字符串转换为字节数组,从字节数组转换为int,并MD5()为每个散列创建新对象,或管理某些静态共享MD5对象().
hashcode ×10
java ×5
c# ×3
equals ×3
string ×2
arrays ×1
collections ×1
dictionary ×1
gethashcode ×1
hash ×1
hive ×1
javascript ×1
md5 ×1
nashorn ×1
php ×1
random ×1
scala ×1
set ×1
sha256 ×1
treemap ×1
treeset ×1
unit-testing ×1