English 中文(简体)
How do I check if a string is a valid md5 or sha1 checksum string
原标题:

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

最佳回答

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}$");
}
问题回答

Any 160-bit sequence is a possible SHA1 hash. Any 128-bit sequence is a possible MD5 hash.

If you re looking at the hex string representations of them, then a sha1 will look like 40 hexadecimal digits, and an md5 will look like 32 hexadecimal digits.

There is no such thing as an MD5 or SHA-1 string, at least not one that is standardized. All you can test for is the size of the byte array: 16 for MD5 (a hash with an output size of 128 bits) or 20 bytes for SHA-1 (a hash with an output size of 160 bits) encoded using hexadecimal encoding or base 64 encoding.

If you use md5sum then generally the checksum is shown as hexadecimal encoding, using only lowercase characters (followed by the file name or - for standard input). Hexadecimals are generally preferred, but hashes may also contain separator character or use a different encoding such as base 64 or base 64 URL (etc. etc.).

The byte size you are testing for might however belong to an entirely different hash such as RIPEMD that may also have the same output size. Or it may be another value with the same amount of bytes.

MD5 verifier:

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

And remove "-" of the string value.

RegExp SHA-1

public static final String SHA_1 = "^([0-9A-Fa-f]{2}[:]){19}([0-9A-Fa-f]{2})$";

public boolean isValidSHA1(String s) {
    return s.matches(SHA_1);
}

boolean isValidSHA1 = isValidSHA1("12:45:54:3A:99:24:52:EA...");




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签