001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util; 017 018import java.io.ByteArrayInputStream; 019import java.io.IOException; 020import java.io.InputStream; 021import java.io.UnsupportedEncodingException; 022import java.security.MessageDigest; 023import java.security.NoSuchAlgorithmException; 024 025public class CheckSumUtils { 026 027 private static final String MD5 = "MD5"; 028 private static final String SHA1 = "SHA1"; 029 private static final String UTF8 = Encodings.UTF8; 030 031 public static String getSHA1Checksum(String message) { 032 return getChecksum(message, SHA1); 033 } 034 035 public static String getMD5Checksum(String message) { 036 return getChecksum(message, MD5); 037 } 038 039 public static String getMD5Checksum(InputStream in) throws IOException { 040 return getChecksum(in, MD5); 041 } 042 043 public static String getSHA1Checksum(InputStream in) throws IOException { 044 return getChecksum(in, SHA1); 045 } 046 047 public static String getChecksum(String message, String algorithm) { 048 try { 049 return getChecksum(getInputStream(message), algorithm); 050 } catch (IOException e) { 051 throw new IllegalStateException("Unexpected IO error", e); 052 } 053 } 054 055 public static String getChecksum(InputStream in, String algorithm) throws IOException { 056 try { 057 byte[] buffer = new byte[1024]; 058 MessageDigest complete = MessageDigest.getInstance(algorithm); 059 int numRead; 060 do { 061 numRead = in.read(buffer); 062 if (numRead > 0) { 063 complete.update(buffer, 0, numRead); 064 } 065 } while (numRead != -1); 066 byte[] bytes = complete.digest(); 067 return toHex(bytes); 068 } catch (NoSuchAlgorithmException e) { 069 throw new IllegalStateException("Unexpected message digest error", e); 070 } catch (IOException e) { 071 throw e; 072 } 073 } 074 075 protected static String toHex(byte[] bytes) { 076 StringBuilder sb = new StringBuilder(); 077 for (int i = 0; i < bytes.length; i++) { 078 sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); 079 } 080 return sb.toString(); 081 } 082 083 protected static InputStream getInputStream(String message) { 084 try { 085 return getInputStream(message, UTF8); 086 } catch (IOException e) { 087 throw new IllegalStateException(e); 088 } 089 } 090 091 protected static InputStream getInputStream(String message, String encoding) throws UnsupportedEncodingException { 092 Assert.noBlanks(message); 093 byte[] bytes = message.getBytes(encoding); 094 return new ByteArrayInputStream(bytes); 095 } 096 097}