4

Possible Duplicate How to reduce a bigger string in smaller string in C++? Probably by hashing?

I have tried to create my own checksum algorithm for a bmp file in java. So, for a 54 byte (432 bit) header, the resulting checksum is 378 bits long.

How can I reduce it to a smaller dataset? Any suggestions on how to implement my own hashing algorithm? (One of the conditions was to not use existing algorithms).

I have used a very simple hash function.

public static String hash_function(String bmpBytes) {

    String hash = "";
    int left_shift = Integer.parseInt(bmpBytes);
    int right_shift = Integer.parseInt(bmpBytes);
    left_shift = left_shift << 2;
    right_shift = right_shift >> 2;
    int xor = left_shift ^ right_shift;
    hash += Integer.toString(xor);
    return hash;
}

POSSIBLE ANSWER :

I found out a way to shorten my string by generating 'n' random binary bits where n < sizeOf(hash) and then doing a hash % n-bits.

If that is a valid answer, do let me know and I shall mark the question as answered.

11
  • What do you need it for? The shorted hashing is to use 0-bits. What is the smallest number of bits you need? Commented Aug 1, 2013 at 18:17
  • I have updated my post to include my hash function. Please check. Commented Aug 1, 2013 at 18:18
  • 1
    You'll have to be more precise on your requirements the algorithm return "" satisfies the ones you have told: the strings is reduced to a smaller one. Should the algorithm be secure, reversible, etc. Commented Aug 1, 2013 at 18:18
  • But it is not clear why you wrote this. Can you state what your requirements are? BTW Storing a hash as text is unusual as it is normally a binary value. Commented Aug 1, 2013 at 18:20
  • The text is actually the checksum generated for the BMP file. My aim is to take the 54 byte header of the BMP file and match it against the checksum to see if it is corrupted or not. Commented Aug 1, 2013 at 18:22

1 Answer 1

4

Here is something simple. Maybe this can inspire you to do something better.

public static String encode(String header) {
    char[] code = new char[32];
    for(int i = 0; i < header.length(); i++) {
        code[i % code.length] = (char)((int)code[i % code.length] ^ (int)header.charAt(i));
    }
    return new String(code);
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is really interesting! Thanks! I shall try and modify this.
how can I get original string back by using that encoded String?
@MuneebNasir usually with hashing this is not possible, one of the reasons for hashing is security, you might use it on a password for one way conversion, although multiple strings map to the same output you can never be sure of the original input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.