/* * (C) Copyright 2015-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Contributors: * ohun@live.cn (夜色) */ package com.mpush.tools.crypto; import com.mpush.api.Constants; import com.mpush.tools.common.IOUtils; import com.mpush.tools.common.Strings; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.security.MessageDigest; /** * Created by ohun on 2015/12/25. * * @author ohun@live.cn */ public final class MD5Utils { public static String encrypt(File file) { InputStream in = null; try { MessageDigest digest = MessageDigest.getInstance("MD5"); in = new FileInputStream(file); byte[] buffer = new byte[1024];//10k int readLen; while ((readLen = in.read(buffer)) != -1) { digest.update(buffer, 0, readLen); } return toHex(digest.digest()); } catch (Exception e) { return Strings.EMPTY; } finally { IOUtils.close(in); } } public static String encrypt(String text) { try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(text.getBytes(Constants.UTF_8)); return toHex(digest.digest()); } catch (Exception e) { return Strings.EMPTY; } } public static String encrypt(byte[] bytes) { try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(bytes); return toHex(digest.digest()); } catch (Exception e) { return Strings.EMPTY; } } private static String toHex(byte[] bytes) { StringBuilder buffer = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; ++i) { buffer.append(Character.forDigit((bytes[i] & 240) >> 4, 16)); buffer.append(Character.forDigit(bytes[i] & 15, 16)); } return buffer.toString(); } public static String hmacSha1(String data, String encryptKey) { final String HMAC_SHA1 = "HmacSHA1"; SecretKeySpec signingKey = new SecretKeySpec(encryptKey.getBytes(Constants.UTF_8), HMAC_SHA1); try { Mac mac = Mac.getInstance(HMAC_SHA1); mac.init(signingKey); mac.update(data.getBytes(Constants.UTF_8)); return toHex(mac.doFinal()); } catch (Exception e) { return Strings.EMPTY; } } public static String sha1(String data) { try { MessageDigest digest = MessageDigest.getInstance("SHA-1"); return toHex(digest.digest(data.getBytes(Constants.UTF_8))); } catch (Exception e) { return Strings.EMPTY; } } }