Java Reference
In-Depth Information
package
stripesbook.nonext;
public class
PasswordTypeConverter
implements
TypeConverter<String> {
public
String convert(String input, Class<?
extends
String> cls,
Collection<ValidationError> errors)
{
return
hash(input);
}
public
String hash(String password) {
try
{
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte
[] bytes = md.digest(password.getBytes());
return
Base64.encodeBytes(bytes);
}
catch
(NoSuchAlgorithmException exc) {
throw
new
IllegalArgumentException(exc);
}
}
public void
setLocale(Locale locale) { }
}
We're using the SHA-1 algorithm to hash the password.
1
Next, let's tell
Stripes to use
PasswordTypeConverter
for the password fields:
@ValidateNestedProperties({
@Validate(field="firstName", required=
true
),
@Validate(field="lastName", required=
true
),
@Validate(field="username", required=
true
),
@Validate(field="password", required=
true
,
converter=PasswordTypeConverter.
class
)
})
private
User user;
@Validate(required=
true
, converter=PasswordTypeConverter.
class
)
private
String confirmPassword;
Now, instead of
"nadia"
, this is the value that will be stored in the
database:
Y9nEX5AlXnnpgnRsC8tAOD8gH8c=
There's not much anyone can do with that sequence of characters!
algorithms.
Search WWH ::
Custom Search