0

I'm translating an api from php to java, i've wrote the following code to emulate sha1 method from php. The problem is that when it creates the new formatter object, I get a FileNotFoundException, debugging I found out that it couldn't find "sunrsasing.jar", looking through stackoverflow I found:

which I'm reading so that before JDK5, it was in a separate jar provided by sun that you mentioned. After that, it was distributed as part of JDK, and not as a separate jar. Blockquote

So my question is, is there any way i can fix the error other than copy & paste the jar from some other source? If not, where can I find it?

private static String encryptPassword(String password)
{
    String sha1 = "";
    try
    {
        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
        crypt.reset();
        crypt.update(password.getBytes("UTF-8"));
        sha1 = byteToHex(crypt.digest());
    }
    catch(NoSuchAlgorithmException e)
    {
        e.printStackTrace();
    }
    catch(UnsupportedEncodingException e)
    {
        e.printStackTrace();
    }
    return sha1;
}



private static String byteToHex(final byte[] hash)
{
    Formatter formatter = new Formatter();
    for (byte b : hash)
    {
        formatter.format("%02x", b);
    }
    String result = formatter.toString();
    formatter.close();
    return result;
}
Ashrith Sheshan
  • 654
  • 4
  • 17
pablo
  • 29
  • 5

1 Answers1

0

java.util.Formatter is available since Java 5, so I guess you are using an older version. For better compatibility you can use a different implementation of your byteToHex() method thst doesn't need that class. See for example How to convert a byte array to a hex string in Java?.

Community
  • 1
  • 1
Pino
  • 7,468
  • 6
  • 50
  • 69