java 對圖片進行MD5演算法

來源:互聯網
上載者:User

在這次項目中需要對圖片MD5來確定一張圖片的唯一性,如果兩個圖片MD5相同就認定問兩張圖片是同一張圖,只不過是被轉載的。

在多次尋找資料後寫出來下面的通用類

 

1、將圖片轉換成位元組

public static String getMD5(String URLName){ String name=""; try { URL url = new URL(URLName); InputStream inputStream = new BufferedInputStream(url.openStream()); byte[] bytes = new byte[1024]; int len = 0; MessageDigest messagedigest = MessageDigest.getInstance("MD5"); while ((len = inputStream.read(bytes)) > 0) { messagedigest.update(bytes, 0, len); } name = MD5Util.bufferToHex(messagedigest.digest()); inputStream.close(); } catch (MalformedURLException e) { LogUtil.getLogger().warn(e); } catch (IOException e) { LogUtil.getLogger().warn(e); } catch (NoSuchAlgorithmException e) { LogUtil.getLogger().warn(e); } return name; } 

有一點很重要 使用了MessageDigest類  下面有該類的介紹

 

2。、調用方法

 

public class MD5Util { /** * 預設的密碼字串組合,用來將位元組轉換成 16 進位表示的字元 */ protected static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6','7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; protected static MessageDigest messagedigest = null; static { try { messagedigest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } public static String getFileMD5String(File file) throws IOException { InputStream fis; fis = new FileInputStream(file); byte[] buffer = new byte[1024]; int numRead = 0; while ((numRead = fis.read(buffer)) > 0) { messagedigest.update(buffer, 0, numRead); } fis.close(); return bufferToHex(messagedigest.digest()); } public static String getStringMD5(String str){ byte[] buffer=str.getBytes(); messagedigest.update(buffer); return bufferToHex(messagedigest.digest()); } public static String bufferToHex(byte bytes[]) { return bufferToHex(bytes, 0, bytes.length); } private static String bufferToHex(byte bytes[], int m, int n) { StringBuffer stringbuffer = new StringBuffer(2 * n); int k = m + n; for (int l = m; l < k; l++) { appendHexPair(bytes[l], stringbuffer); } return stringbuffer.toString(); } private static void appendHexPair(byte bt, StringBuffer stringbuffer) { char c0 = hexDigits[(bt & 0xf0) >> 4];// 取位元組中高 4 位的數字轉換 // 為邏輯右移,將符號位一起右移,此處未發現兩種符號有何不同 char c1 = hexDigits[bt & 0xf];// 取位元組中低 4 位的數字轉換 stringbuffer.append(c0); stringbuffer.append(c1); } }  

 

 

 

 

Java 加密技術:訊息摘要。

一個訊息摘要就是一個資料區塊的數位指紋。即對一個任意長度的一個資料區塊進行計算,產生一個唯一指印(對於SHA1是產生一個20位元組的位元組)。

訊息摘要有兩個基本屬性:

兩個不同的報文難以產生相同的摘要 
難以對指定的摘要產生一個報文,而由該報文反推算出該指定的摘要
代表:美國國家標準技術研究所的SHA1和麻省理工學院Ronald Rivest提出的MD5

  類 java.security.MessageDigest

java.lang.Object   |   +----java.security.MessageDigest
public abstract class  MessageDigest extends  Object

MessageDigest 提供了訊息摘要演算法,如 MD5 或 SHA,的功能。訊息摘要是安全單向散列函數,它採用任意大小的資料並輸出一個固定長度的散列值。

象 Java 安全性中的其它基於演算法的類一樣,MessageDigest 有兩個主要的組件: 訊息摘要 API ( 應用程式介面 ) 這是需要訊息摘要服務的應用調用的方法的介面。這個 API 由所有公有方法組成。 訊息摘要 SPI ( 服務提供者介面 ) 該介面是由提供特殊演算法的提供者實現的介面。它由所有名字首碼為  engine 的方法組成。每個這樣的方法由具有相應名字的公有 API 方法調用。例如, engineReset 方法由  reset 方法調用。SPI 方法是抽象的;提供者必須提供一個具體的實現。

MessageDigest 對象在啟動時被初始化。使用 update 方法處理資料。在任何地方都可調用 reset 複位摘要。一旦所有需要修改的資料都被修改了,將調用一個 digest 方法完成散列碼的計算。

對於給定次數的修改,只能調用 digest 方法一次。在調用 digest 之後,MessageDigest 對象被複位為初始化的狀態。 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.