I’m using eclipselink as JPA Implementation and PostgreSQL and want to store SHA-1 Hashes in the database.
But I’m getting an Exception like this:
Internal Exception: org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding “UTF8”: 0x00
Error Code: 0
Call: INSERT INTO mbm_user (USERNAME, PRENAME, LASTNAME, PASSWORD) VALUES (?, ?, ?, ?)
bind => [Hans, null, null, THE SHA-1 HASH]
THE SHA-1 HASH are some gibberish chars and I can’t paste them here.
My Entity:
@Entity
@Table(name="mbm_user")
public class User extends CanAccessBook{
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(nullable = false)
private Long id;
@Column(nullable = false)
private String username;
@Column(nullable = false)
private String password;
private String prename;
private String lastname;
...
...
}
And the method where I generate the SHA-1 Hash:
public void setPassword(String password) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] encryptPassword = md.digest(password.getBytes());
this.password = new String(encryptPassword);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
How can i avoid the usage of the illegal char 0x00 or is the problem somewhere else?
You are trying to cram a byte[] into a String, which is never going to work well.
@Lob @Column(nullable = false) private byte[] passwordHash;