BCryptハッシュとは
Blowfishは鍵を利用したブロック暗号方式で、現時点で十分な暗号化強度を有しています。
また、その他の暗号化方式が独占的で特許などにより保護されていたのに比べて、Blowfishは独占的な権利を主張しておらず、以下のように自由な利用が保証されています。
Blowfish is unpatented, and will remain so in all countries. The algorithm is hereby placed in the public domain, and can be freely used by anyone.
https://en.wikipedia.org/wiki/Bcrypt
Javaでの利用
Javaの標準APIに見つからなかったので、他のライブラリを探したところSpring Securityによる実装が良さそうだったので、この方法を採用しました。
MavenでSpring Security Coreを導入します。
pom.xml
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
暗号化と比較
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
// 略
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String encodedPassword = encoder.encode("password");
if(bcpe.matches("password", encodedPassword)){
System.out.println("match");
}else{
System.out.println("unmatch");
}
簡単すぎて衝撃!!






