HBASE-24163 MOB compactor implementations should use format specifiers when calling...
[hbase.git] / hbase-common / src / main / java / org / apache / hadoop / hbase / util / MD5Hash.java
blobc38f1a9f8b9573b1789eed69f2c5e6e6063dbf93
1 /**
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 package org.apache.hadoop.hbase.util;
22 import java.security.MessageDigest;
23 import java.security.NoSuchAlgorithmException;
25 import org.apache.commons.codec.binary.Hex;
26 import org.apache.yetus.audience.InterfaceAudience;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 /**
31 * Utility class for MD5
32 * MD5 hash produces a 128-bit digest.
34 @InterfaceAudience.Public
35 public class MD5Hash {
36 private static final Logger LOG = LoggerFactory.getLogger(MD5Hash.class);
38 /**
39 * Given a byte array, returns in MD5 hash as a hex string.
40 * @param key
41 * @return SHA1 hash as a 32 character hex string.
43 public static String getMD5AsHex(byte[] key) {
44 return getMD5AsHex(key, 0, key.length);
47 /**
48 * Given a byte array, returns its MD5 hash as a hex string.
49 * Only "length" number of bytes starting at "offset" within the
50 * byte array are used.
52 * @param key the key to hash (variable length byte array)
53 * @param offset
54 * @param length
55 * @return MD5 hash as a 32 character hex string.
57 public static String getMD5AsHex(byte[] key, int offset, int length) {
58 try {
59 MessageDigest md = MessageDigest.getInstance("MD5");
60 md.update(key, offset, length);
61 byte[] digest = md.digest();
62 return new String(Hex.encodeHex(digest));
63 } catch (NoSuchAlgorithmException e) {
64 // this should never happen unless the JDK is messed up.
65 throw new RuntimeException("Error computing MD5 hash", e);