Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 531
yossis Avatar asked Dec 13 '09 14:12

yossis


People also ask

How do I check if a string is an MD5?

You can check using the following function: function isValidMd5($md5 ='') { return preg_match('/^[a-f0-9]{32}$/', $md5); } echo isValidMd5('5d41402abc4b2a76b9719d911017c592'); The MD5 (Message-digest algorithm) Hash is typically expressed in text format as a 32 digit hexadecimal number.

How do I find MD5?

Type the following command: md5sum [type file name with extension here] [path of the file] -- NOTE: You can also drag the file to the terminal window instead of typing the full path. Hit the Enter key. You'll see the MD5 sum of the file. Match it against the original value.


1 Answers

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}$"); } 
like image 159
dfa Avatar answered Sep 30 '22 06:09

dfa