Java – GlassFish security – jdbcrealm: how to configure login using SHA-256 summary

I use JDBC realm to protect my GlassFish v3 0.1 b22. It is set to use the user table in my database for authentication, following the following blog: http://blogs.oracle.com/foo/entry/mort_learns_jdbc_realm_authentication. If I use the digest algorithm as plain text, I can work normally However, when I tried to use Sha - 256 for summarization algorithm, it stopped working

Here is my simple java program:

MessageDigest md = MessageDigest.getInstance("SHA-256");
        String text = "admin";
        md.update(text.getBytes("UTF-8"));
        byte[] digest = md.digest();
        System.out.println(digest.toString());

Solution

JDBC realm allows encoding hex or Base64 values You need to specify one of them in your domain configuration and code to convert the byte array to one of the following formats:

Base64 encoding:

import com.sun.org.apache.xml.internal.security.utils.Base64;
...
byte[] digest = md.digest();
System.out.println(Base64.encode(digest));

hexadecimal:

...
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < digest.length; i++) {
    String hex = Integer.toHexString(0xff & digest[i]);
    if (hex.length() == 1) sb.append('0');
    sb.append(hex);
}
System.out.println(sb.toString());

You need to increase the size of the password field Sha - 256 Base64 and hex values are 45 and 64 characters respectively

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>