「Java」文字列のSHA-256のハッシュ値を取得するサンプル
pom.xml
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
</dependencies>
<dependencies> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.15</version> </dependency> </dependencies>
書式
DigestUtils.sha256Hex(対象文字列)
使用例
package com.arkgame.study;
import org.apache.commons.codec.digest.DigestUtils;
public class SHADemo {
private static String targetA = "テスト";
private static String targetB = "テスト123";
public static void main(String[] args) {
String resA = DigestUtils.sha256Hex(targetA);
System.out.println("ハッシュ値1:" + resA);
String resB = DigestUtils.sha256Hex(targetB);
System.out.println("ハッシュ値2:" + resB);
}
}
package com.arkgame.study;
import org.apache.commons.codec.digest.DigestUtils;
public class SHADemo {
private static String targetA = "テスト";
private static String targetB = "テスト123";
public static void main(String[] args) {
String resA = DigestUtils.sha256Hex(targetA);
System.out.println("ハッシュ値1:" + resA);
String resB = DigestUtils.sha256Hex(targetB);
System.out.println("ハッシュ値2:" + resB);
}
}
package com.arkgame.study; import org.apache.commons.codec.digest.DigestUtils; public class SHADemo { private static String targetA = "テスト"; private static String targetB = "テスト123"; public static void main(String[] args) { String resA = DigestUtils.sha256Hex(targetA); System.out.println("ハッシュ値1:" + resA); String resB = DigestUtils.sha256Hex(targetB); System.out.println("ハッシュ値2:" + resB); } }
実行結果
ハッシュ値1:8a535a3f4dcd2c396db11b7c1e54221d04375c9f9be96bce47dc2fdb237e86c9
ハッシュ値2:1c7891fbb65acbef09e6b8624992976601ea8bffb58a38614b42b069c17a05a8