Better encoding handling and library_id based on md5

This patch corrects some inconsitence in encoding handling and generate
a library_id based on a md5 string instead of auto increment