~ubuntu-branches/ubuntu/vivid/tomcat8/vivid-proposed

« back to all changes in this revision

Viewing changes to java/org/apache/tomcat/util/security/MD5Encoder.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2014-09-29 13:23:43 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20140929132343-c2xxjsnwh125m6h1
Tags: 8.0.14-1
* New upstream release
  - Refreshed the patches
* Build depend on libcglib3-java instead of libcglib-java
* Standards-Version updated to 3.9.6 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 * contributor license agreements.  See the NOTICE file distributed with
 
4
 * this work for additional information regarding copyright ownership.
 
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 * (the "License"); you may not use this file except in compliance with
 
7
 * the License.  You may obtain a copy of the License at
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 * Unless required by applicable law or agreed to in writing, software
 
12
 * distributed under the License is distributed on an "AS IS" BASIS,
 
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 * See the License for the specific language governing permissions and
 
15
 * limitations under the License.
 
16
 */
 
17
package org.apache.tomcat.util.security;
 
18
 
 
19
/**
 
20
 * Encode an MD5 digest into a String.
 
21
 * <p>
 
22
 * The 128 bit MD5 hash is converted into a 32 character long String.
 
23
 * Each character of the String is the hexadecimal representation of 4 bits
 
24
 * of the digest.
 
25
 *
 
26
 * @author Remy Maucherat
 
27
 */
 
28
public final class MD5Encoder {
 
29
 
 
30
 
 
31
    private MD5Encoder() {
 
32
        // Hide default constructor for utility class
 
33
    }
 
34
 
 
35
 
 
36
    private static final char[] hexadecimal = {'0', '1', '2', '3', '4', '5',
 
37
        '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
 
38
 
 
39
 
 
40
    /**
 
41
     * Encodes the 128 bit (16 bytes) MD5 into a 32 character String.
 
42
     *
 
43
     * @param binaryData Array containing the digest
 
44
     *
 
45
     * @return Encoded MD5, or null if encoding failed
 
46
     */
 
47
    public static String encode(byte[] binaryData) {
 
48
 
 
49
        if (binaryData.length != 16)
 
50
            return null;
 
51
 
 
52
        char[] buffer = new char[32];
 
53
 
 
54
        for (int i=0; i<16; i++) {
 
55
            int low = binaryData[i] & 0x0f;
 
56
            int high = (binaryData[i] & 0xf0) >> 4;
 
57
            buffer[i*2] = hexadecimal[high];
 
58
            buffer[i*2 + 1] = hexadecimal[low];
 
59
        }
 
60
 
 
61
        return new String(buffer);
 
62
    }
 
63
}
 
64