How to use BigInteger in Java - 2
May 31, 2023
1 min
A hash function is a one-way function that maps arbitrary data of any length to data of a fixed length.
The unidirectional means that decoding is impossible. It is widely used for data and file integrity verification and encryption.
Types of hash functions mainly used include MD5, SHA-1, SHA-256, and SHA-512.
stringBuilder = new StringBuilder();md5 = MessageDigest.getInstance("MD5");md5.update(target.getBytes());digest = md5.digest();for (byte b : digest) {stringBuilder.append(String.format("%02x", b));}result = stringBuilder.toString();
stringBuilder = new StringBuilder();md = MessageDigest.getInstance("SHA-256");md.update(target.getBytes());digest = md.digest();for (byte b : digest) {stringBuilder.append(String.format("%02x", b));}result = stringBuilder.toString();
String data = "target_data";String hex = null; //result datatry {MessageDigest msg = MessageDigest.getInstance("SHA-512");msg.update(data.getBytes());hex = String.format("%128x", new BigInteger(1, msg.digest()));} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return hex;