Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a hash as byte array with JPA

My User entity class contains password hash field, which is a byte array with a fixed length (32 since it's an SHA-256 hash).

@Entity
public class User {
    @Column(nullable=false)
    private byte[] passwordHash;
    ...
}

As you can see, I haven't annotated it with anything special, just a NOT NULL.

This works, but will it perform? My schema is generated by Hibernate, but I don't know exactly what it generates (I'm currently using an in-memory HSQL database).

I'm concerned that, since it doesn't know that it's a fixed length array (the length field of the Column annotation applies to strings only), it will store this hash in a BLOB field which is added in the record as a pointer (if I understand correctly how databases work).

Is this true, and how can I change this? Should I just encode the hash as a string, with base64 or hex, accepting the small performance/correctness impact of that?

like image 249
Bart van Heukelom Avatar asked Sep 29 '10 12:09

Bart van Heukelom


1 Answers

I'm concerned that, since it doesn't know that it's a fixed length array (the length field of the Column annotation applies to strings only), (...)

If you specify the column length, Hibernate will use this information to determine the SQL column type to generate (TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB) though.

What I need is BINARY(32)

Did you try this?

@Column(columnDefinition="BINARY(32) NOT NULL")
private byte[] passwordHash;
like image 58
Pascal Thivent Avatar answered Sep 23 '22 11:09

Pascal Thivent