Java MD5 algorithm for pictures

Source: Internet
Author: User

In this project need to picture MD5 to determine the uniqueness of a picture, if two pictures MD5 the same on the identification of two pictures is the same map, but is reproduced.

After you've searched for data multiple times, write down the generic class below

1. Convert a picture into a binary array

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 (message Digest.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; }

It's important to use the MessageDigest class below for an introduction to this class

2. , calling Method

 

The public class Md5util {/** * default password string combination used to convert bytes into the 16-notation character */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 ( ); The 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); Buffertohex (Messagedigest.digest ()); public static string Buffertohex (Byte bytes[]) {return Buffertohex (bytes, 0, bytes.length);} private static String buf Fertohex (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]; Take the number of high 4 digits in the byte to move to the logical right, move the symbol bit to the right, and there is no difference between the two symbols. Char c1 = HEXDIGITS[BT & 0xf];//A numeric conversion stringbuffer.append with a lower 4 digits in the byte ( C0); Stringbuffer.append (C1); }}  

Java encryption technology: Message digest.

A message digest is a digital fingerprint of a block of data. That is, a data block of any length is computed to produce a unique fingerprint (for SHA1 is to produce a binary array of 20 bytes).

The message digest has two basic properties:

Two different messages are difficult to generate the same summary
It is difficult to generate a message for the specified digest, and the specified summary is inferred from the message
Representative: SHA1 of the American Institute of National Standards and technology and Ronald Rivest of MIT MD5

class Java.security.MessageDigest

Java.lang.Object
   |
   +----Java.security.MessageDigest
Public abstract class MessageDigestExtends Object

MessageDigest provides a message digest algorithm, such as MD5 or SHA, for functionality. A message digest is a secure one-way hash function that takes data of any size and outputs a fixed-length hash value.

Like other algorithmic classes in Java security, MessageDigest has two main components: the message digest API (Application interface) This is the interface of the method that requires the application invocation of the Message Digest service. This API is made up of all public methods. Message Digest SPI (Service Provider Interface) This interface is an interface implemented by a provider that provides a special algorithm. It consists of all name prefixes for engine methods. Each such method is called by the public API method with the corresponding name. For example, the Enginereset method is called by the Reset method. The SPI method is abstract; The provider must provide a concrete implementation.

The MessageDigest object is initialized at startup. Use the Update method to process data. Reset Reset summary can be called anywhere. Once all the data that needs to be modified is modified, a digest method is called to complete the hash code calculation.

For a given number of changes, only the digest method can be invoked once. After the call to digest, the MessageDigest object is reset to the initialized state.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.