Java Reference
In-Depth Information
Download email_34/src/stripesbook/nonext/PasswordTypeConverter.java
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:
Download email_34/src/stripesbook/action/RegisterActionBean.java
@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!
1. See http://en.wikipedia.org/wiki/SHA for more information on SHA and other hashing
algorithms.
 
Search WWH ::




Custom Search