SHA256 in Java

Posted on 2007-08-01
Last Modified: 2013-11-23
I've found a JavaScript that implements SHA256, the encoded64 string that it returns is fully compatible with the result I've got with C# cryptograhpy API, so I can hash strings on web browser, send them to the server and compare against C# hashed strings.

Now I'm traying to do the same with Java but the API seems to produce a very different result. I thought that, if sha256 is a "standard" algoritm, results has to be the same.

What can be happened? Are there any JavaScript "compatible" with Java API?

I'm using MessageDigest.getInstance("SHA-256") to do the hashing

Question by:mak730
    LVL 4

    Expert Comment

    Could you give more details on your code ?

    Right now, i assume that you should do the followings:


    hash = MessageDigest.getInstance("SHA-256");
    // Calculate the hash
    byte[] digest = hash.digest(input);
    // Base64-encode it before returning
    return Base64.encodeBytes(digest);

    If this is what you have done you should get the same result.

    Author Comment

    Yes it is exactly what I'm doing, but, for documentation that I read, it seems (I'm not sure) that Java does not have the SHA256 algorithm so I guess it is taken SHA1. Do you know if the framework has, in fact,  built in support for SHA256?
    LVL 4

    Accepted Solution

    So far, there is not common standard for JCE(Java Cryptography Extension) implementation. The implementation (the jar file you choose ) might not cover SHA-256 algorithm.

    Check here, and choose the one that suit you most

    As far as i know, SHA-1 and SHA-256 is quite similar. They only differs in the message size.

    If you choose SHA-1, you would have more implementation choices since most implementation covered SHA-1


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Title # Comments Views Activity
    Tips on building some plugins or small utility apps 3 54
    scoresIncreasing challenge 10 48
    changePi Challenge 15 56
    array6 challenfge 6 48
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now