001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 * 027 * This file is based on: 028 * - org.apache.catalina.util.MD5Encoder 029 * from the Apache Tomcat project. 030 * 031 * Licensed to the Apache Software Foundation (ASF) under one or more 032 * contributor license agreements. See the NOTICE file distributed with 033 * this work for additional information regarding copyright ownership. 034 * The ASF licenses this file to You under the Apache License, Version 2.0 035 * (the "License"); you may not use this file except in compliance with 036 * the License. You may obtain a copy of the License at 037 * 038 * http://www.apache.org/licenses/LICENSE-2.0 039 * 040 * Unless required by applicable law or agreed to in writing, software 041 * distributed under the License is distributed on an "AS IS" BASIS, 042 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 043 * See the License for the specific language governing permissions and 044 * limitations under the License. 045 */ 046 047package org.opencms.webdav; 048 049/** 050 * Encode an MD5 digest into a String.<p> 051 * 052 * The 128 bit MD5 hash is converted into a 32 character long String. 053 * Each character of the String is the hexadecimal representation of 4 bits 054 * of the digest. 055 * 056 * @since 6.5.6 057 */ 058public final class CmsMD5Encoder { 059 060 /** Characters of hexadecimal numbers. */ 061 private static final char[] HEXADECIMAL = { 062 '0', 063 '1', 064 '2', 065 '3', 066 '4', 067 '5', 068 '6', 069 '7', 070 '8', 071 '9', 072 'a', 073 'b', 074 'c', 075 'd', 076 'e', 077 'f'}; 078 079 /** 080 * Encodes the 128 bit (16 bytes) MD5 into a 32 character String.<p> 081 * 082 * @param binaryData array containing the digest 083 * 084 * @return encoded MD5, or null if encoding failed 085 */ 086 public String encode(byte[] binaryData) { 087 088 if (binaryData.length != 16) { 089 return null; 090 } 091 092 char[] buffer = new char[32]; 093 094 for (int i = 0; i < 16; i++) { 095 int low = (binaryData[i] & 0x0f); 096 int high = ((binaryData[i] & 0xf0) >> 4); 097 buffer[i * 2] = HEXADECIMAL[high]; 098 buffer[(i * 2) + 1] = HEXADECIMAL[low]; 099 } 100 101 return new String(buffer); 102 } 103 104}