ハマログ

株式会社イーツー・インフォの社員ブログ

JavaでBCrypt(Blowfish)ハッシュ処理と比較を利用する(Spring Security)

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");
}

簡単すぎて衝撃!!

BCryptBlowfishJavamd5passwordハッシュパスワードパスワードハッシュ

  kaneko tomo   2017年2月26日


関連記事

新しくなったGoogleサイト

Googleサイトが新しくなりました。 従来のGoogleサイト。古いかんじです…

ITツールの機能紹介-Gmailで時間指定で検索する方法-

つ~じ~です。巨星墜つ、と言える大きな出来事が続いていて驚きを隠せません。時代の…

最近、個人的にバズってるアプリを紹介

どうも!こんにちは! 家にPCモニターが無いため安いものを探しているWingma…


← 前の投稿

次の投稿 →