Java MD5 isEtagAlsoAnMD5Hash(String etag)

Here you can find the source of isEtagAlsoAnMD5Hash(String etag)

Description

Guess whether the given ETag value is also an MD5 hash of an underlying object in a storage service, as opposed to being some other kind of opaque hash.

License

Apache License

Return

true if the ETag value can be assumed to also be an MD5 hash.

Declaration

public static boolean isEtagAlsoAnMD5Hash(String etag) 

Method Source Code

//package com.java2s;
/*//  w w  w.  jav  a2 s  . c om
 * JetS3t : Java S3 Toolkit
 * Project hosted at http://bitbucket.org/jmurty/jets3t/
 *
 * Copyright 2006-2010 James Murty
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Guess whether the given ETag value is also an MD5 hash of an underlying object
     * in a storage service, as opposed to being some other kind of opaque hash.
     * <p>
     * This test was made necessary by Amazon S3's multipart upload feature, where
     * the ETag value returned after a re-assembled multipart upload is completed
     * is no longer the same as an MD5 hash of the assembled data.
     * <p>
     * An ETag is considered also an MD5 when:
     * <ul>
     * <li>The length is exactly 16 characters (excluding surrounding quote characters)</li>
     * <li>All characters in the string are hexadecimal values, i.e. [0-9a-f] when lowercased</li>
     * </ul>
     * <p>
     * These rules are drawn from the post by Carl@AWS on Nov 11, 2010 10:40 AM here:
     * https://forums.aws.amazon.com/thread.jspa?messageID=222158&tstart=0
     *
     * @return
     * true if the ETag value can be assumed to also be an MD5 hash.
     */
    public static boolean isEtagAlsoAnMD5Hash(String etag) {
        if (etag == null || etag.length() != 32) {
            return false;
        }
        String nonHexChars = etag.toLowerCase().replaceAll("[a-f0-9]", "");
        if (nonHexChars.length() > 0) {
            return false;
        }
        return true;
    }
}

Related

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