2

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. I need something similar working (and returning the same values for the same inputs) in java, php and 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());
    }

}
Addev
  • 31,819
  • 51
  • 183
  • 302

2 Answers2

4

What you are looking for is Checksum methods. MD5 is one of the Hashing technique that will give you always the same output across platforms and languages. I think you will have to do your own research to figure out APIs in each language.

But here are some good starts.

And many more

Community
  • 1
  • 1
Sap
  • 5,197
  • 8
  • 59
  • 101
2

Relying on Java's .hashCode() is not what you want here. 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.

Pick your poison!

fge
  • 119,121
  • 33
  • 254
  • 329
  • So do I combine all the values in a string and then generate a MD5? – Addev Jun 19 '13 at 07:07
  • @Addev Why don't you write a code example of what you are trying to achieve – Sap Jun 19 '13 at 07:10
  • @Addev that would be a solution but then be careful, choose a separator which you are sure will never appear in a string; if for instance you have strings `foobar` and `baz` on one side, and `foo` and `barbaz` on the other, do not concatenate: they will have the same answer – fge Jun 19 '13 at 07:20