1 19 package com.softabar.sha4j; 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 27 50 public class ShaUtil 51 { 52 private static Sha4J sha4j=new Sha4J(); 53 55 public static byte[] sha1(String input) throws IOException 56 { 57 sha4j.reset(); 58 return sha4j.sha1Digest(new ByteArrayInputStream (input.getBytes())); 59 60 } 61 62 public static byte[] sha1(InputStream input) throws IOException 63 { 64 sha4j.reset(); 65 return sha4j.sha1Digest(input); 66 } 67 68 public static byte[] sha1(File input) throws IOException 69 { 70 sha4j.reset(); 71 FileInputStream fis=new FileInputStream (input); 72 return sha4j.sha1Digest(fis); 73 } 74 75 public static String toSha1String(String input) throws IOException 76 { 77 return toHexString(sha1(input)); 78 } 79 80 public static String toSha1String(InputStream input) throws IOException 81 { 82 return toHexString(sha1(input)); 83 } 84 85 public static String toSha1String(File input) throws IOException 86 { 87 return toHexString(sha1(input)); 88 } 89 90 92 public static byte[] sha224(String input) throws IOException 93 { 94 sha4j.reset(); 95 return sha4j.sha224Digest(new ByteArrayInputStream (input.getBytes())); 96 97 } 98 99 public static byte[] sha224(InputStream input) throws IOException 100 { 101 sha4j.reset(); 102 return sha4j.sha224Digest(input); 103 } 104 105 public static byte[] sha224(File input) throws IOException 106 { 107 sha4j.reset(); 108 FileInputStream fis=new FileInputStream (input); 109 return sha4j.sha224Digest(fis); 110 } 111 112 public static String toSha224String(String input) throws IOException 113 { 114 return toHexString(sha224(input)); 115 } 116 117 public static String toSha224String(InputStream input) throws IOException 118 { 119 return toHexString(sha224(input)); 120 } 121 122 public static String toSha224String(File input) throws IOException 123 { 124 return toHexString(sha224(input)); 125 } 126 127 128 130 public static byte[] sha256(String input) throws IOException 131 { 132 sha4j.reset(); 133 return sha4j.sha256Digest(new ByteArrayInputStream (input.getBytes())); 134 135 } 136 137 public static byte[] sha256(InputStream input) throws IOException 138 { 139 sha4j.reset(); 140 return sha4j.sha256Digest(input); 141 } 142 143 public static byte[] sha256(File input) throws IOException 144 { 145 sha4j.reset(); 146 FileInputStream fis=new FileInputStream (input); 147 return sha4j.sha256Digest(fis); 148 } 149 150 public static String toSha256String(String input) throws IOException 151 { 152 return toHexString(sha256(input)); 153 } 154 155 public static String toSha256String(InputStream input) throws IOException 156 { 157 return toHexString(sha256(input)); 158 } 159 160 public static String toSha256String(File input) throws IOException 161 { 162 return toHexString(sha256(input)); 163 } 164 165 167 public static byte[] sha384(String input) throws IOException 168 { 169 sha4j.reset(); 170 return sha4j.sha384Digest(new ByteArrayInputStream (input.getBytes())); 171 172 } 173 174 public static byte[] sha384(InputStream input) throws IOException 175 { 176 sha4j.reset(); 177 return sha4j.sha384Digest(input); 178 } 179 180 public static byte[] sha384(File input) throws IOException 181 { 182 sha4j.reset(); 183 FileInputStream fis=new FileInputStream (input); 184 return sha4j.sha384Digest(fis); 185 } 186 187 public static String toSha384String(String input) throws IOException 188 { 189 return toHexString(sha384(input)); 190 } 191 192 public static String toSha384String(InputStream input) throws IOException 193 { 194 return toHexString(sha384(input)); 195 } 196 197 public static String toSha384String(File input) throws IOException 198 { 199 return toHexString(sha384(input)); 200 } 201 202 public static byte[] sha512(String input) throws IOException 204 { 205 sha4j.reset(); 206 return sha4j.sha512Digest(new ByteArrayInputStream (input.getBytes())); 207 208 } 209 210 public static byte[] sha512(InputStream input) throws IOException 211 { 212 sha4j.reset(); 213 return sha4j.sha512Digest(input); 214 } 215 216 public static byte[] sha512(File input) throws IOException 217 { 218 sha4j.reset(); 219 FileInputStream fis=new FileInputStream (input); 220 return sha4j.sha512Digest(fis); 221 } 222 223 public static String toSha512String(String input) throws IOException 224 { 225 return toHexString(sha512(input)); 226 } 227 228 public static String toSha512String(InputStream input) throws IOException 229 { 230 return toHexString(sha512(input)); 231 } 232 233 public static String toSha512String(File input) throws IOException 234 { 235 return toHexString(sha512(input)); 236 } 237 238 239 240 241 243 private static final char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; 244 245 250 public static String toHexString(byte[] b) 251 { 252 StringBuffer sb = new StringBuffer (); 253 254 for (int i = 0; i < b.length; i++) 255 { 256 int c = ((b[i]) >>> 4) & 0xf; 257 sb.append(hex[c]); 258 c = ((int) b[i] & 0xf); 259 sb.append(hex[c]); 260 } 261 262 return sb.toString(); 263 } 264 } 265 | Popular Tags |