简体   繁体   English

跨平台哈希码

[英]Cross platform hashcode

I need to create a checksum consisting in several objects of basic types. 我需要创建一个包含几个基本类型对象的校验和。 I read the " Writing a correct hashCode method " section in THIS page. 我在页面中阅读了“ 编写正确的hashCode方法 ”部分。 I need something similar working (and returning the same values for the same inputs) in java, php and objectivec. 我需要在Java,PHP和Objectivec中进行类似的工作(并为相同的输入返回相同的值)。

How can I do it? 我该怎么做? is there some library I can use? 我可以使用一些图书馆吗?

Edit (my current code): 编辑(我当前的代码):

public class CheckSumGenerator {

    private final static String SEPARATOR = "|";
    private final static String DOUBLE_FORMAT = "%.30f";
    private final static DecimalFormat FORMAT_DOUBLE=new DecimalFormat("#.#################################");

    StringBuilder tempChain = new StringBuilder();

    public void putInt(int value) {
        tempChain.append(SEPARATOR).append(value);
    }

    public void putLong(long value) {
        tempChain.append(SEPARATOR).append(value);
    }

    public void putString(String value) {
        tempChain.append(SEPARATOR).append(value);
    }

    public void putBoolean(boolean value) {
        tempChain.append(SEPARATOR).append(value ? 1 : 0);
    }

    public void putDouble(double value) {
        tempChain.append(SEPARATOR).append(FORMAT_DOUBLE.format(value));
    }

    public String getChecksum() {
        return HashUtils.MD5(tempChain.toString());
    }

}

What you are looking for is Checksum methods. 您正在寻找的是Checksum方法。 MD5 is one of the Hashing technique that will give you always the same output across platforms and languages. MD5是一种哈希技术,可为您提供跨平台和语言始终相同的输出。 I think you will have to do your own research to figure out APIs in each language. 我认为您必须进行自己的研究才能找出每种语言的API。

But here are some good starts. 但是这里有一些好的开始。

And many more 还有很多

Relying on Java's .hashCode() is not what you want here. 依赖Java的.hashCode()并不是您想要的。 If you want checksums, you have the choice of using, for instance, MD5, which is available in pretty much all languages, or SHA1, SHA256, SHA512 etc. 如果需要校验和,则可以选择使用MD5,例如几乎所有语言都可用的MD5或SHA1,SHA256,SHA512等。

Pick your poison! 选择你的毒药!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM