SHA-256 ハッシュ値を計算する
src/main/java/com/example/Example.java
package com.example;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Example {
public static void main(String[] args) throws NoSuchAlgorithmException {
MessageDigest digest= MessageDigest.getInstance("SHA-256");
byte[] bytes = digest.digest("ABCDEFG".getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
System.out.println(sb.toString());
}
}
実行結果
$ mvn -q compile exec:java -Dexec.mainClass=com.example.Example
e9a92a2ed0d53732ac13b031a27b071814231c8633c9f41844ccba884d482b16
MD5 ハッシュ値を計算する
src/main/java/com/example/Example.java
package com.example;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Example {
public static void main(String[] args) throws NoSuchAlgorithmException {
MessageDigest digest= MessageDigest.getInstance("MD5");
byte[] bytes = digest.digest("ABCDEFG".getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
System.out.println(sb.toString());
}
}
実行結果
$ mvn -q compile exec:java -Dexec.mainClass=com.example.Example
bb747b3df3130fe1ca4afa93fb7d97c9