0

I got the following code:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class Sha1{

    private static final char[] HEX_CHARS = null;

    public static void main(String[] args){
        String hash = toSHA1(("27"+"peojvootv").getBytes());
        System.out.println(hash);
    }
    public static String toSHA1(byte[] convertme) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
        }
        catch(NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        byte[] buf = md.digest(convertme);
        char[] chars = new char[2 * buf.length];
        for (int i = 0; i < buf.length; ++i) {
            chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
            chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
        }
        return new String(chars);
    }
}

Somehow it issues an error. I dont know how to fix it. this is the callstack

Exception in thread "main" java.lang.NullPointerException
    at mainClockies.Sha1.toSHA1(Sha1.java:26)//The return statement of second method
    at mainClockies.Sha1.main(Sha1.java:12)//The callback of the second method
einstein
  • 13,389
  • 27
  • 80
  • 110
  • Don't swallow the `NoSuchAlgorithmException` exception. If it is ever thrown, you are guaranteed to get an NPE. Better to wrap it in a RuntimeException and let it bubble up – Adam Batkin Mar 01 '11 at 18:17
  • Do you mean wrap `try{}` with whole method body and then `catch(Exception e){ e.printStackTrace();}`? – einstein Mar 01 '11 at 18:34

3 Answers3

4

Well, it would happen if MessageDigest.getInstance() throw NoSuchAlgorithmException - because you're printing out the exception but then carrying on regardless.

However, it's actually happening because of this:

private static final char[] HEX_CHARS = null;

and then this:

chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];

I suspect you're not actually running the code you've got in front of you - on my machine at least, the NPE correctly points to line 24, the line including HEX_CHARS.

To fix:

private static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • ok thanks! I just took the code from http://stackoverflow.com/questions/4895523/java-string-to-sha1, the last example and implemented it. And my eclipse auto-fixed the Hex-Chars with `private static final char[] HEX_CHARS = null;` I replaced that line with your line. And now I recieve the right result. But I wonder if this code is optimal for a SHA1 hash-function? – einstein Mar 01 '11 at 18:32
  • @Woho87: It's okay, as far as I'm aware - although you should definitely do something about that catch block. You should probably throw a RuntimeException. – Jon Skeet Mar 01 '11 at 18:36
  • Do you mean wrap `try{}` with whole method body and then `catch(Exception e){ e.printStackTrace();}`? – einstein Mar 01 '11 at 18:44
  • @Woho87: No, I mean get rid of `e.printStackTrace();` in the current code and replace it with `throw new RuntimeException(e);`. Swallowing an exception and just pretending it didn't happen is almost *never* the right approach. – Jon Skeet Mar 01 '11 at 18:46
2

Your HEX_CHARS variable is never set to anything besides null.

Richard Pianka
  • 3,317
  • 2
  • 28
  • 36
0

Try putting something other than null in HEX_CHARS.

bmargulies
  • 97,814
  • 39
  • 186
  • 310