How do I check if a string is a valid md5 or sha1 checksum string

yossis picture yossis · Dec 13, 2009 · Viewed 19.2k times · Source

I don't want to calculate a file's checksum, just to know if a given string is a valid checksum

Answer

dfa picture dfa · Dec 13, 2009

SHA1 verifier:

public boolean isValidSHA1(String s) {
    return s.matches("^[a-fA-F0-9]{40}$");
}

MD5 verifier:

public boolean isValidMD5(String s) {
    return s.matches("^[a-fA-F0-9]{32}$");
}