ハマログ

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

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日


関連記事

ITツールの機能紹介-SeleniumIDEの技術開拓 execute scriptの比較計算処理がうまくいかなかったお話

つ~じ~です。近年、アニメのリメイクや数十年ぶりの新作ラッシュがやばいですね。う…

スプレッドシートテクニック – ショートカット集

つーじーです。先日、人生初の内視鏡検査に行ってきました。ポリープ切除しました。最…

ITツールの機能紹介-GoogleChromeのタブグループ機能-

つーじーです。名前が「つーじー」「つ~じ~」と表記揺れがあることに最近気づきまし…


← 前の投稿

次の投稿 →