<commit_before>package com.peterphi.std.crypto;

import org.bouncycastle.crypto.generators.OpenBSDBCrypt;

import java.security.SecureRandom;
import java.util.Random;

/**
 * @deprecated use {@link OpenBSDBCrypt} instead
 */
@Deprecated
public class BCrypt
{
	// BCrypt parameters
	private static final int DEFAULT_COST = 10;
	private static final int BCRYPT_SALT_LEN = 16;


	/**
	 * Check that a plaintext password matches a previously hashed one
	 *
	 * @param password
	 * 		the plaintext password to verify
	 * @param hash
	 * 		the previously-hashed password (a 60-character BCrypt hash string)
	 *
	 * @return true if the passwords match, false otherwise
	 */

	private static boolean verify(String hash, char[] password)
	{
		return OpenBSDBCrypt.checkPassword(hash, password);
	}


	/**
	 * Hash a password using the OpenBSD bcrypt scheme
	 *
	 * @param password
	 * 		the plaintext to test
	 * @param salt
	 * 		the salt value to use
	 * @param cost
	 * 		the computation cost (complexity increases at <code>2**cost</code>)
	 *
	 * @return a 60-character BCrypt hash string
	 */
	private static String hashpw(char[] password, byte[] salt, int cost)
	{
		return OpenBSDBCrypt.generate(password, salt, cost);
	}


	/**
	 * Hash a password using the OpenBSD bcrypt scheme
	 *
	 * @param password
	 * 		the password to hash
	 * @param salt
	 * 		the salt to hash with (perhaps generated using BCrypt.gensalt)
	 *
	 * @return the hashed password
	 */
	public static String hashpw(char[] password, byte[] salt)
	{
		return hashpw(password, salt, DEFAULT_COST);
	}


	/**
	 * Generate a salt for use with the {@link #hashpw(char[], byte[])} method
	 *
	 * @param random
	 * 		the random number generator to use
	 *
	 * @return an encoded salt value
	 */
	public static byte[] gensalt(Random random)
	{
		final byte rnd[] = new byte[BCRYPT_SALT_LEN];

		random.nextBytes(rnd);

		return rnd;
	}


	/**
	 * Generate a salt for use with the {@link #hashpw(char[], byte[])} method
	 *
	 * @return an encoded salt value
	 */
	public static byte[] gensalt()
	{
		return gensalt(new SecureRandom());
	}
}
<commit_msg>Further simplify the BCrypt wrapper<commit_after>package com.peterphi.std.crypto;

import org.bouncycastle.crypto.generators.OpenBSDBCrypt;

import java.security.SecureRandom;
import java.util.Random;

/**
 * A wrapper around {@link OpenBSDBCrypt}
 */
public class BCrypt
{
	public static final int DEFAULT_COST = 12;

	/**
	 * The size of the BCrypt salt (fixed value)
	 */
	private static final int BCRYPT_SALT_LEN = 16;


	/**
	 * @param password
	 * 		the plaintext password
	 * @param cost
	 * 		the computation cost (complexity increases at <code>2**cost</code>)
	 *
	 * @return
	 */
	public static String hash(char[] password, int cost)
	{
		final byte[] salt = gensalt(new SecureRandom());

		return hash(password, salt, cost);
	}


	/**
	 * Check that a plaintext password matches a previously hashed one
	 *
	 * @param password
	 * 		the plaintext password to verify
	 * @param hash
	 * 		the previously-hashed password (a 60-character BCrypt hash string)
	 *
	 * @return true if the passwords match, false otherwise
	 */
	public static boolean verify(final String hash, final char[] password)
	{
		return OpenBSDBCrypt.checkPassword(hash, password);
	}


	/**
	 * Hash a password using the OpenBSD bcrypt scheme
	 *
	 * @param password
	 * 		the plaintext to hash
	 * @param salt
	 * 		the salt value to use
	 * @param cost
	 * 		the computation cost (complexity increases at <code>2**cost</code>)
	 *
	 * @return a 60-character BCrypt hash string
	 */
	public static String hash(final char[] password, final byte[] salt, final int cost)
	{
		return OpenBSDBCrypt.generate(password, salt, cost);
	}


	/**
	 * Generate a salt for use with the {@link #hash(char[], byte[], int)} method
	 *
	 * @param random
	 * 		the random number generator to use
	 *
	 * @return an encoded salt value
	 */
	public static byte[] gensalt(final Random random)
	{
		final byte rnd[] = new byte[BCRYPT_SALT_LEN];

		random.nextBytes(rnd);

		return rnd;
	}
}

{'fraction_non_alphanumeric': 0.10435638690622692, 'fraction_numerical': 0.004676347526458282, 'mean_word_length': 3.465934065934066, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3136332', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1223, 'n_words': 434}