จะรับ MD5 checksum สำหรับไฟล์ที่ระบุใน Java ได้อย่างไร จะใช้ยูทิลิตี้ DigestUtils.md5Hex ของ Apache Common ได้อย่างไร
เผยแพร่แล้ว: 2021-08-28
การทำให้แอปพลิเคชันองค์กรของคุณมีความปลอดภัยคือความท้าทายที่ยิ่งใหญ่ที่สุดขององค์กร
พิจารณาสถานการณ์จำลองการผลิตจริงนี้:
- คุณมีแอปพลิเคชันที่
reads the value
จากไฟล์ - ขึ้นอยู่กับค่าของไฟล์ มัน
performs some operations
เช่น add/delete/execute - คุณได้ปรับใช้แอปพลิเคชันนี้ในสภาพแวดล้อมการผลิต
- จะเกิดอะไรขึ้นหากมี
unauthorized person
changes the value
ของไฟล์นั้นโดยที่คุณไม่รู้ตัว - แอปพลิเคชันของคุณ
simply gets new value
จากไฟล์และเรียกใช้ตรรกะซึ่งอาจทำให้เกิดunexpected outcome
- หากคุณเปิดใช้งาน MD5 checksum สำหรับไฟล์นั้น – คุณ
could have created an exception
พร้อมข้อความแสดงข้อผิดพลาดที่ชัดเจน และyou could have prevented disaster
หรือผลลัพธ์ที่ไม่คาดคิดได้
เช็คซัม MD5 คืออะไร?
การตรวจสอบ MD5 สำหรับไฟล์มีค่า 128-bit value
ซึ่งคล้ายกับลายนิ้วมือของไฟล์ มีประโยชน์ทั้งในการเปรียบเทียบไฟล์และการควบคุมความสมบูรณ์ของไฟล์
แทบจะเป็นไปไม่ได้เลยที่จะได้ค่าเดียวกันสำหรับไฟล์ที่ต่างกัน ในบทช่วยสอนนี้ เราจะสร้างโปรแกรม Java อย่างง่าย ซึ่งสร้างและคืนค่า MD5 สำหรับไฟล์ที่กำหนด ในกรณีของเราคือไฟล์ index.php
มาเริ่มกันเลย:
ขั้นตอนที่ 1
สร้างคลาสสาธารณะ CrunchifyGetMD5ForFile.java
ขั้นตอนที่ 2
นำเข้าการขึ้นต่อกันของ maven สองรายการไปยังไฟล์ pom.xml
ของโปรเจ็กต์ของคุณ บทช่วยสอนการแปลงโปรเจ็กต์เป็นโปรเจ็กต์ maven
1 2 3 4 5 6 7 8 9 10 |
< dependency > < groupId > commons - codec < / groupId > < artifactId > commons - codec < / artifactId > < version > 1.10 < / version > < / dependency > < dependency > < groupId > commons - io < / groupId > < artifactId > commons - io < / artifactId > < version > 2.4 < / version > < / dependency > |
เรากำลังใช้ไลบรารี่ commons-codec
และ commons-io
ขั้นตอนที่ 3
สร้างเช็คซัม MD5 โดยใช้ยูทิลิตี้ DigestUtils.md5Hex
และพิมพ์ผลลัพธ์บนคอนโซล
DigestUtils.md5Hex => encodeHex Implementation
1 2 3 4 5 6 7 8 9 10 |
protected static char [ ] encodeHex ( final byte [ ] data , final char [ ] toDigits ) { final int l = data . length ; final char [ ] out = new char [ l < < 1 ] ; // two characters form the hex value. for ( int i = 0 , j = 0 ; i < l ; i ++ ) { out [ j ++ ] = toDigits [ ( 0xF0 & data[i]) >>> 4]; out [ j ++ ] = toDigits [ 0x0F & data[i]]; } return out ; } |
นี่คือโปรแกรมเต็มรูปแบบ
สร้างคลาส crunchifyGetMd5ForFile.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
package crunchify . com . tutorials ; import org . apache . commons . codec . digest . DigestUtils ; import org . apache . commons . io . IOUtils ; import java . io . File ; import java . io . FileInputStream ; import java . io . IOException ; /** * @author Crunchify.com * How to get MD5 checksum for any given file in Java? Use commons-codec's DigestUtils.md5Hex. */ public class CrunchifyGetMD5ForFile { public static String crunchifyGetMd5ForFile ( String crunchifyFile ) { // File(): Creates a new File instance by converting the given pathname string into an abstract pathname. // If the given string is the empty string, then the result is the empty abstract pathname. File myFile = new File ( crunchifyFile ) ; return crunchifyGetMd5ForFile ( myFile ) ; } public static String crunchifyGetMd5ForFile ( File crunchifyFile ) { String crunchifyValue = null ; // FileInputStream: A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. // FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. FileInputStream crunchifyInputStream = null ; try { crunchifyInputStream = new FileInputStream ( crunchifyFile ) ; // md5Hex converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. // The returned array will be double the length of the passed array, as it takes two characters to represent any given byte. // DigestUtils(): Operations to simplify common MessageDigest tasks. This class is immutable and thread-safe. // However the MessageDigest instances it creates generally won't be. // The MessageDigestAlgorithms class provides constants for standard digest algorithms that can be // used with the getDigest(String) method and other methods that require the Digest algorithm name. // toByteArray(): Get the contents of an InputStream as a byte[]. // This method buffers the input internally, so there is no need to use a BufferedInputStream. crunchifyValue = DigestUtils . md5Hex ( IOUtils . toByteArray ( crunchifyInputStream ) ) ; // md5Hex(): Calculates the MD5 digest and returns the value as a 32 character hex string. } catch ( IOException e ) { log ( "Hey there is an error: " + e ) ; } finally { // closeQuietly(): Unconditionally close an InputStream. // Equivalent to InputStream.close(), except any exceptions will be ignored. // This is typically used in finally blocks. IOUtils . closeQuietly ( crunchifyInputStream ) ; } return crunchifyValue ; } // Simple log util private static void log ( String string ) { System . out . println ( string ) ; } public static void main ( String [ ] agrs ) { // Let's get MD5 for File index.php located at /Users/app/Download/ String file = "//cdn.crunchify.com/Users/app/Download/crunchify.png" ; String md5Value = crunchifyGetMd5ForFile ( file ) ; log ( "File: " + file + " \nValue: " + md5Value ) ; } } |
เพียงเรียกใช้โปรแกรมด้านบนเป็น Java Application แล้วคุณจะเห็นผลลัพธ์ที่คล้ายคลึงกันนี้

ผลลัพธ์คอนโซล IntelliJ IDEA:
1 2 3 4 |
File : / Users / app / Download / crunchify . png Value : 103e7ae51e641d674780f7a03b491321 Process finished with exit code 0 |
คุณจะใช้เช็คซัม MD5 นี้ ณ รันไทม์เพื่อตรวจสอบความสมบูรณ์ของไฟล์ได้อย่างไร
คุณสามารถเปรียบเทียบเช็คซัม MD5 นี้ที่รันไทม์กับค่าที่เก็บไว้ในฐานข้อมูล y0ur เช่น MySQL, Oracle เป็นต้น
มีหลายวิธีที่คุณสามารถบรรลุสิ่งเดียวกันได้ แต่เราจะพูดถึงเรื่องนี้ในบทช่วยสอนในอนาคต