データベースに保存するためにパスワードをハッシュする必要があります。Javaでこれを行うにはどうすればよいですか?
プレーンテキストのパスワードを取得し、ランダムなソルトを追加してから、ソルトとハッシュ化されたパスワードをデータベースに保存したいと考えていました。
次に、ユーザーがログインしたいときに、送信されたパスワードを取得し、アカウント情報からランダムなソルトを追加してハッシュし、保存されているハッシュパスワードとアカウント情報が一致するかどうかを確認します。
データベースに保存するためにパスワードをハッシュする必要があります。Javaでこれを行うにはどうすればよいですか?
プレーンテキストのパスワードを取得し、ランダムなソルトを追加してから、ソルトとハッシュ化されたパスワードをデータベースに保存したいと考えていました。
次に、ユーザーがログインしたいときに、送信されたパスワードを取得し、アカウント情報からランダムなソルトを追加してハッシュし、保存されているハッシュパスワードとアカウント情報が一致するかどうかを確認します。
実際には、Javaランタイムに組み込まれている機能を使用してこれを行うことができます。Java 6のSunJCEは、パスワードハッシュに使用するのに適したアルゴリズムであるPBKDF2をサポートしています。
byte[] salt = new byte[16];
random.nextBytes(salt);
KeySpec spec = new PBEKeySpec("password".toCharArray(), salt, 65536, 128);
SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hash = f.generateSecret(spec).getEncoded();
Base64.Encoder enc = Base64.getEncoder();
System.out.printf("salt: %s%n", enc.encodeToString(salt));
System.out.printf("hash: %s%n", enc.encodeToString(hash));
PBKDF2パスワード認証に使用できるユーティリティクラスは次のとおりです。
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Arrays;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
/**
* Hash passwords for storage, and test passwords against password tokens.
*
* Instances of this class can be used concurrently by multiple threads.
*
* @author erickson
* @see <a href="http://stackoverflow.com/a/2861125/3474">StackOverflow</a>
*/
public final class PasswordAuthentication
{
/**
* Each token produced by this class uses this identifier as a prefix.
*/
public static final String ID = "$31$";
/**
* The minimum recommended cost, used by default
*/
public static final int DEFAULT_COST = 16;
private static final String ALGORITHM = "PBKDF2WithHmacSHA1";
private static final int SIZE = 128;
private static final Pattern layout = Pattern.compile("\\$31\\$(\\d\\d?)\\$(.{43})");
private final SecureRandom random;
private final int cost;
public PasswordAuthentication()
{
this(DEFAULT_COST);
}
/**
* Create a password manager with a specified cost
*
* @param cost the exponential computational cost of hashing a password, 0 to 30
*/
public PasswordAuthentication(int cost)
{
iterations(cost); /* Validate cost */
this.cost = cost;
this.random = new SecureRandom();
}
private static int iterations(int cost)
{
if ((cost < 0) || (cost > 30))
throw new IllegalArgumentException("cost: " + cost);
return 1 << cost;
}
/**
* Hash a password for storage.
*
* @return a secure authentication token to be stored for later authentication
*/
public String hash(char[] password)
{
byte[] salt = new byte[SIZE / 8];
random.nextBytes(salt);
byte[] dk = pbkdf2(password, salt, 1 << cost);
byte[] hash = new byte[salt.length + dk.length];
System.arraycopy(salt, 0, hash, 0, salt.length);
System.arraycopy(dk, 0, hash, salt.length, dk.length);
Base64.Encoder enc = Base64.getUrlEncoder().withoutPadding();
return ID + cost + '$' + enc.encodeToString(hash);
}
/**
* Authenticate with a password and a stored password token.
*
* @return true if the password and token match
*/
public boolean authenticate(char[] password, String token)
{
Matcher m = layout.matcher(token);
if (!m.matches())
throw new IllegalArgumentException("Invalid token format");
int iterations = iterations(Integer.parseInt(m.group(1)));
byte[] hash = Base64.getUrlDecoder().decode(m.group(2));
byte[] salt = Arrays.copyOfRange(hash, 0, SIZE / 8);
byte[] check = pbkdf2(password, salt, iterations);
int zero = 0;
for (int idx = 0; idx < check.length; ++idx)
zero |= hash[salt.length + idx] ^ check[idx];
return zero == 0;
}
private static byte[] pbkdf2(char[] password, byte[] salt, int iterations)
{
KeySpec spec = new PBEKeySpec(password, salt, iterations, SIZE);
try {
SecretKeyFactory f = SecretKeyFactory.getInstance(ALGORITHM);
return f.generateSecret(spec).getEncoded();
}
catch (NoSuchAlgorithmException ex) {
throw new IllegalStateException("Missing algorithm: " + ALGORITHM, ex);
}
catch (InvalidKeySpecException ex) {
throw new IllegalStateException("Invalid SecretKeyFactory", ex);
}
}
/**
* Hash a password in an immutable {@code String}.
*
* <p>Passwords should be stored in a {@code char[]} so that it can be filled
* with zeros after use instead of lingering on the heap and elsewhere.
*
* @deprecated Use {@link #hash(char[])} instead
*/
@Deprecated
public String hash(String password)
{
return hash(password.toCharArray());
}
/**
* Authenticate with a password in an immutable {@code String} and a stored
* password token.
*
* @deprecated Use {@link #authenticate(char[],String)} instead.
* @see #hash(String)
*/
@Deprecated
public boolean authenticate(String password, String token)
{
return authenticate(password.toCharArray(), token);
}
}
BCryptは非常に優れたライブラリであり、そのJavaポートがあります。
PBKDF2、BCrypt、SCrypt、およびArgon2パスワード暗号化をサポートするSpring Security Crypto(オプションのコンパイル依存関係は2つのみ)を使用できます。
Argon2PasswordEncoder argon2PasswordEncoder = new Argon2PasswordEncoder();
String aCryptedPassword = argon2PasswordEncoder.encode("password");
boolean passwordIsValid = argon2PasswordEncoder.matches("password", aCryptedPassword);
SCryptPasswordEncoder sCryptPasswordEncoder = new SCryptPasswordEncoder();
String sCryptedPassword = sCryptPasswordEncoder.encode("password");
boolean passwordIsValid = sCryptPasswordEncoder.matches("password", sCryptedPassword);
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
String bCryptedPassword = bCryptPasswordEncoder.encode("password");
boolean passwordIsValid = bCryptPasswordEncoder.matches("password", bCryptedPassword);
Pbkdf2PasswordEncoder pbkdf2PasswordEncoder = new Pbkdf2PasswordEncoder();
String pbkdf2CryptedPassword = pbkdf2PasswordEncoder.encode("password");
boolean passwordIsValid = pbkdf2PasswordEncoder.matches("password", pbkdf2CryptedPassword);
を使用してハッシュを計算できますMessageDigest
が、これはセキュリティの観点からは間違っています。ハッシュは簡単に解読できるため、パスワードの保存には使用しないでください。
パスワードを保存するには、bcrypt、PBKDF2、scryptなどの別のアルゴリズムを使用する必要があります。ここを参照してください。
OWASPで記述されているもののShiroライブラリ(以前のJSecurity)の実装を使用できます。
また、JASYPTライブラリにも同様のユーティリティがあるようです。
PBKDF2が答えであるというエリクソンに完全に同意します。
そのオプションがない場合、またはハッシュのみを使用する必要がある場合、Apache Commons DigestUtilsは、JCEコードを正しく取得するよりもはるかに簡単です: https ://commons.apache.org/proper/commons-codec/apidocs/org/apache /commons/codec/digest/DigestUtils.html
ハッシュを使用する場合は、sha256またはsha512を使用してください。このページには、パスワード処理とハッシュに関する適切な推奨事項があります(パスワード処理にハッシュを推奨しないことに注意してください): http ://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html
他の回答で言及されているbcryptとPBKDF2に加えて、scryptを調べることをお勧めします
MD5とSHA-1は比較的高速であるため、「1時間あたりのレント」分散コンピューティング(EC2など)または最新のハイエンドGPUを使用すると、ブルートフォース/辞書攻撃を使用して比較的低コストで合理的にパスワードを「解読」できるため、お勧めしません。時間。
それらを使用する必要がある場合は、少なくともアルゴリズムを事前定義されたかなりの回数(1000回以上)繰り返します。
詳細については、こちらをご覧ください:https ://security.stackexchange.com/questions/211/how-to-securely-hash-passwords
そしてここに:http://codahale.com/how-to-safely-store-a-password/(パスワードハッシュの目的でSHAファミリー、MD5などを批判します)
NISTの推奨PBKDF2についてはすでに言及しましたが、2013年から2015年にかけて公開パスワードハッシュコンテストが開催されたことを指摘しておきます。最終的に、推奨パスワードハッシュ関数としてArgon2が選択されました。
使用できる元の(ネイティブC)ライブラリにはかなりよく採用されているJavaバインディングがあります。
平均的なユースケースでは、Argon2よりもPBKDF2を選択した場合、またはその逆の場合、セキュリティの観点からは問題ではないと思います。強力なセキュリティ要件がある場合は、評価でArgon2を検討することをお勧めします。
パスワードハッシュ関数のセキュリティの詳細については、security.seを参照してください。
ここに、MD5ハッシュと他のハッシュメソッドの2つのリンクがあります。
Javadoc API:https ://docs.oracle.com/javase/1.5.0/docs/api/java/security/MessageDigest.html
すべての標準ハッシュスキームの中で、LDAPsshaは最も安全に使用できます。
http://www.openldap.org/faq/data/cache/347.html
そこで指定されているアルゴリズムに従い、MessageDigestを使用してハッシュを実行します。
あなたが提案したようにあなたはあなたのデータベースに塩を保存する必要があります。
2020年の時点で、使用されている最も信頼性の高いパスワードハッシュアルゴリズムは、ハードウェアを考慮してその強度を最適化する可能性が最も高く、Argon2idまたはArgon2iですが、Springの実装ではありません。
PBKDF2標準には、ブロック暗号BCRYPTアルゴのCPU欲張り/計算コストの高い機能が含まれており、そのストリーム暗号機能が追加されています。PBKDF2は、指数関数的に貪欲なSCRYPTに圧倒され、次にサイドチャネル攻撃に強いArgon2に圧倒されました。
Argon2は、ターゲットのハッシュ時間と使用されるハードウェアを指定して、最適化された強度パラメーターを見つけるために必要なキャリブレーションツールを提供します。
メモリ貪欲なハッシュは、クラッキングのためのGPUの使用を防ぐのに役立ちます。
春のセキュリティ/弾力がある城の実装は最適化されておらず、攻撃者が使用できるものを考えると比較的1週間です。cf:SpringdocArgon2とScrypt
現在の実装では、パスワードクラッカーが行う並列処理/最適化を利用しない弾力がある城を使用しているため、攻撃者と防御者の間に不必要な非対称性があります。
Javaで使用されている最も信頼できる実装は、mkammererのものです。
Cで記述された公式のネイティブ実装のラッパーjar/ライブラリ。
それはよく書かれていて、使いやすいです。
組み込みバージョンは、Linux、Windows、およびOSXのネイティブビルドを提供します。
例として、それは、クォーラム、そのイーサリアム暗号通貨の実装を保護するために使用されるテセラセキュリティプロジェクトでjpmorganchaseによって使用されます。
次に例を示します。
final char[] password = "a4e9y2tr0ngAnd7on6Pà§§M°RD".toCharArray();
byte[] salt = new byte[128];
new SecureRandom().nextBytes(salt);
final Argon2Advanced argon2 = Argon2Factory.createAdvanced(Argon2Factory.Argon2Types.ARGON2id);
byte[] hash = argon2.rawHash(10, 1048576, 4, password, salt);
(テセラを参照)
POMでlibを宣言します。
<dependency>
<groupId>de.mkammerer</groupId>
<artifactId>argon2-jvm</artifactId>
<version>2.7</version>
</dependency>
またはgradle付き:
compile 'de.mkammerer:argon2-jvm:2.7'
キャリブレーションは、 de.mkammerer.argon2.Argon2Helper#findIterationsを使用して実行できます
SCRYPTおよびPbkdf2アルゴリズムは、いくつかの単純なベンチマークを作成することによって調整することもできますが、現在の最小の安全な反復値では、より長いハッシュ時間が必要になります。
私はそれをudemyのビデオから借りて、より強力なランダムパスワードになるように編集しました
}
private String pass() {
String passswet="1234567890zxcvbbnmasdfghjklop[iuytrtewq@#$%^&*" ;
char icon1;
char[] t=new char[20];
int rand1=(int)(Math.random()*6)+38;//to make a random within the range of special characters
icon1=passswet.charAt(rand1);//will produce char with a special character
int i=0;
while( i <11) {
int rand=(int)(Math.random()*passswet.length());
//notice (int) as the original value of Math>random() is double
t[i] =passswet.charAt(rand);
i++;
t[10]=icon1;
//to replace the specified item with icon1
}
return new String(t);
}
}