anurodh sahu
anurodh sahu

Reputation: 123

Is md5 value language dependent?

If I generate md5 of a file in java and javascript -Will both the values be same? I have to generate md5 in javascript and send it to the server and the server matches it. I am getting a hash mismatch error.

Actually, I need 'base64 encoded 128-bit MD5 digest' to send to S3 using javascript.

Thanks

Upvotes: 2

Views: 264

Answers (3)

Roshana Pitigala
Roshana Pitigala

Reputation: 8806

NO. MD5 is language independent.

But as I understood you are creating the hash code from one computer and reading it using another, which may cause into a charset chaos. Make sure you are using the same charset UTF-8 recommended. And also I found this helpful, try using this without storing the hash in your database.

public static String MD5(String md5) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            byte[] array = md.digest(md5.getBytes("UTF-8"));
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
            }
            return sb.toString();
        } catch (Exception e) {
        }
        return null;
    }

Upvotes: 0

UKAgr
UKAgr

Reputation: 11

MD5 is just an hashing algorithm and hence can be implemented in any language. Result will be the same, as long as you don't make any mistake in the implementation.

Some areas to take care of, while implementing in different languages, which could unintentionally introduce errors in implementation, could be: range/Truncating/promotion of integer values, operators precedence etc.

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240966

md5 is the hashing algorithm, It is language independent. as long as the input to the algorithm is same it will generate same hash

Upvotes: 4

Related Questions