Java MD5 looksLikeValidMd5(String possibleMd5)

Here you can find the source of looksLikeValidMd5(String possibleMd5)

Description

Is the argument a legitimate-seeming MD5 value?

License

Apache License

Parameter

Parameter Description
possibleMd5 the supposed MD5 hash to check

Return

true if the parameter is a plausible MD5 value

Declaration

public static boolean looksLikeValidMd5(String possibleMd5) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from  w w w .j  a v  a2 s  .c  o m*/
     * MD5 hash values are 32 characters long.
     */
    private static final int MD5_LENGTH = 32;

    /**
     * Is the argument a legitimate-seeming MD5 value?
     * That is, is it the right length (32 characters), and does it consist only of hex digits?
     *
     * @param possibleMd5 the supposed MD5 hash to check
     * @return true if the parameter is a plausible MD5 value
     */
    public static boolean looksLikeValidMd5(String possibleMd5) {
        return (possibleMd5 != null) && possibleMd5.length() == MD5_LENGTH
                && possibleMd5.chars().allMatch(c -> isHex((char) c));
    }

    /**
     * Is the argument character a valid hexadecimal digit?
     * @param c the candidate character
     * @return true if it's hex, false otherwise
     */
    public static boolean isHex(char c) {
        return Character.isDigit(c) || (('a' <= c) && (c <= 'f')) || (('A' <= c) && (c <= 'F'));
    }
}

Related

  1. getMD5Hex(byte[] digest)
  2. getMD5Prefix()
  3. getMd5Script(String dstFilePath)
  4. inputStreamMD5(InputStream entity)
  5. isEtagAlsoAnMD5Hash(String etag)
  6. md5(File f)
  7. md5(final String text)
  8. md5(final String... tokens)
  9. md5(String source)