Can't get to have SHA-256 hash working with my spring security

两盒软妹~` 提交于 2019-12-03 09:12:32

Finally I came up with this solution, thank to @Oleg Estekhin comment:

public String sha256(String original) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(original.getBytes());
    byte[] digest = md.digest();
    return new String(Hex.encodeHexString(digest));
}

However, the hashed password in the configuration file is 8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918 but the one stored in the > database is a weird set of characters when I inspect my mysql database via console or ivAMgsKo*H when displayed in a page.

8c6976e5b541041... 

looks like a Base64 encoded data.

While

 ivAMgsKo*H

and your code

new String(digest, "UTF-8")

tells me, that your digest is not encoded but stored as a UTF-8 string.

A hash value can contain not printable byte values, so it is common to encode the value. For example with Base64.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!