本文整理汇总了Java中com.lambdaworks.crypto.SCryptUtil类的典型用法代码示例。如果您正苦于以下问题:Java SCryptUtil类的具体用法?Java SCryptUtil怎么用?Java SCryptUtil使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SCryptUtil类属于com.lambdaworks.crypto包,在下文中一共展示了SCryptUtil类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testRegisterNewUser
import com.lambdaworks.crypto.SCryptUtil; //导入依赖的package包/类
/**
* Integration test that stores User into the database, then reads it from the database
* and verifies if password was correctly hashed.
*/
@Transactional
@Test
public void testRegisterNewUser() {
final String userName = "someone";
final String password = "somepass";
userService.registerNewUser(new User("[email protected]", userName, password));
User user = userRepository.findByUserName(userName);
Assert.assertNotNull(user);
Assert.assertTrue(SCryptUtil.check(password, user.getPassword()));
//Verifying interaction with a mocked bean
verify(statisticsService, times(1)).recalculateStatisticReports();
}
示例2: updateUserPassword
import com.lambdaworks.crypto.SCryptUtil; //导入依赖的package包/类
public void updateUserPassword(int id, String oldPassword, String newPassword) {
final String scrypt = SCryptUtil.scrypt(newPassword, 2 << 14, 8, 1);
if (!PASSWORD_PATTERN.matcher(newPassword).matches()) {
throw new RakamException("Password is not valid. Your password must contain at least one lowercase character, uppercase character and digit and be at least 8 characters. ", BAD_REQUEST);
}
if (config.getHashPassword()) {
oldPassword = CryptUtil.encryptWithHMacSHA1(oldPassword, encryptionConfig.getSecretKey());
}
try (Handle handle = dbi.open()) {
String hashedPass = handle.createQuery("SELECT password FROM web_user WHERE id = :id")
.bind("id", id).map(StringMapper.FIRST).first();
if (hashedPass == null) {
throw new RakamException("User does not exist", BAD_REQUEST);
}
if (!SCryptUtil.check(oldPassword, hashedPass)) {
throw new RakamException("Password is wrong", BAD_REQUEST);
}
handle.createStatement("UPDATE web_user SET password = :password WHERE id = :id")
.bind("id", id)
.bind("password", scrypt).execute();
}
}
示例3: checkInputs
import com.lambdaworks.crypto.SCryptUtil; //导入依赖的package包/类
private boolean checkInputs() {
boolean result = true;
if (masterPassword.getText() == null || masterPassword.getText().toString().equals("")) {
result = false;
masterPassword.setError(getActivity().getString(R.string.errorEmpty));
} else if (fullName.getText() == null || fullName.getText().toString().equals("")) {
result = false;
fullName.setError(getActivity().getString(R.string.errorEmpty));
} else if (defaultPrefs.verifyPassword()) {
try {
result = SCryptUtil
.check(masterPassword.getText().toString(), defaultPrefs.masterPasswordHash());
if (!result) {
masterPassword.setError(getActivity().getString(R.string.error_incorrectPassword));
}
} catch (Exception e) {
result = false;
defaultPrefs.masterPasswordHash(null);
defaultPrefs.verifyPassword(false);
VerificationFailedDialog verificationFailedDialog = new VerificationFailedDialog();
verificationFailedDialog.show(getActivity().getSupportFragmentManager(), null);
}
}
return result;
}
示例4: changePassword
import com.lambdaworks.crypto.SCryptUtil; //导入依赖的package包/类
@ExpectPermission(Permission.UPDATE_PROFILE)
@RequestMapping(value = "/api/self/password", method = RequestMethod.POST)
public int changePassword(UserWithPermission user, @RequestBody PasswordChange passwordChange) {
String currentHashedPassword = userRepository.getHashedPassword(user.getProvider(), user.getUsername());
Validate.isTrue(SCryptUtil.check(passwordChange.getCurrentPassword(), currentHashedPassword));
return userService.changePassword(user.getId(), passwordChange.getNewPassword());
}
示例5: encode
import com.lambdaworks.crypto.SCryptUtil; //导入依赖的package包/类
@Override
public String encode(String password) {
long time = System.currentTimeMillis();
String result = SCryptUtil.scrypt(password, complexity, 8, 1);
log.info("It took {} ms to encode the password.", System.currentTimeMillis() - time);
return result;
}
示例6: verify
import com.lambdaworks.crypto.SCryptUtil; //导入依赖的package包/类
public boolean verify(String password, PasswordResponse response) {
if (response.getAlgorithm() == Algorithm.BCRYPT) {
return BCrypt.checkpw(password, response.getHash());
}
if (response.getAlgorithm() == Algorithm.SCRYPT) {
return SCryptUtil.check(password, response.getHash());
}
if (response.getAlgorithm() == Algorithm.PBKDF2) {
if (response.getAlgorithmDetails().getIterations() > MAX_PKBDF2_ITERATIONS) {
throw new IllegalArgumentException("Iterations should not exceed " + MAX_PKBDF2_ITERATIONS);
}
byte[] hash = Base64.decodeBase64(response.getHash());
PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), response.getSalt().getBytes(), response
.getAlgorithmDetails().getIterations(), response.getAlgorithmDetails().getKeySize());
try {
SecretKeyFactory skf = PBKDF2Algorithms.getSecretKeyFactory("PBKDF2WithHmac" + response.getAlgorithmDetails().getHashFunction().replace("-", ""));
byte[] expectedHash = skf.generateSecret(spec).getEncoded();
return Arrays.equals(hash, expectedHash);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
return true;
}
示例7: scryptTest
import com.lambdaworks.crypto.SCryptUtil; //导入依赖的package包/类
@Test
public void scryptTest() {
String password = PasswordUtils.getRandomPassword();
String hash = SCryptUtil.scrypt(password, 2, 2, 2);
PasswordResponse response = new PasswordResponse();
response.setAlgorithm(Algorithm.SCRYPT);
response.setHash(hash);
Assert.assertTrue(verifier.verify(password, response));
Assert.assertFalse(verifier.verify(password + " ", response));
}
示例8: Exception
import com.lambdaworks.crypto.SCryptUtil; //导入依赖的package包/类
public static Boolean check
( String Passphrase
, String Hash
, String Algorithm
)
throws Exception
{
switch
( Algorithm == null
? Utils.identifyAlgorithm(Hash)
: Utils.Algorithm.fromString(Algorithm)
)
{
case bcrypt:
return BCrypt.checkpw
( Passphrase
, Hash
);
case pbkdf2:
return PasswordHash.validatePassword
( Passphrase
, Hash
);
case scrypt:
return SCryptUtil.check
( Passphrase
, Hash
);
default:
throw new Exception("Unsupported Algorithm");
}
}
示例9: authenticate
import com.lambdaworks.crypto.SCryptUtil; //导入依赖的package包/类
private boolean authenticate(final String username, final String password) {
String hashed = userRepository.getHashedPassword(USER_PROVIDER, username);
return SCryptUtil.check(password, hashed);
}
示例10: hashPassword
import com.lambdaworks.crypto.SCryptUtil; //导入依赖的package包/类
private static String hashPassword(String password) {
return SCryptUtil.scrypt(password, 2 << 14, 8, 1);
}