Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8003961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:47:28+00:00 2026-06-04T16:47:28+00:00

What I’m trying to do is to convert a password typed into a jPasswordField

  • 0

What I’m trying to do is to convert a password typed into a jPasswordField into SHA-256 hash. I was wandering around and found how to do this if I’ve got password saved as a string but the field I’m using is returning char[] so I ended up just guessing what to do… at first I’ve got different results even if the password was the same but now I believe I’m closer as it’s a constant; but it’s still not it as the output of

echo -n 'abc' | sha256sum

is

ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

while the output of my action (for the same input) is

86900f25bd2ee285bc6c22800cfb8f2c3411e45c9f53b3ba5a8017af9d6b6b05

My action looks like this:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        NoSuchAlgorithmException noSuchAlgorithmException = null;
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");

        } catch (NoSuchAlgorithmException ex) {
            noSuchAlgorithmException = ex;
        }
        if (noSuchAlgorithmException != null) {
            System.out.println(noSuchAlgorithmException.toString());
        }
        else {
            UnsupportedEncodingException unsupportedEncodingException = null;
            byte[] hash = null;
            char[] password = jPasswordField1.getPassword();
            StringBuffer stringBuffer = new StringBuffer();
            for (char c : password) {
                if (c > 0 && c < 16) {
                    stringBuffer.append("0");
                }
                stringBuffer.append(Integer.toHexString(c & 0xff));
            }
            String passwordString = stringBuffer.toString();
            try {
                hash = messageDigest.digest(passwordString.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException ex) {
                unsupportedEncodingException = ex;
            }
            if (unsupportedEncodingException != null) {
                System.out.println(unsupportedEncodingException.toString());
            }
            else {
                stringBuffer = new StringBuffer();
                for (byte b : hash) {
                    stringBuffer.append(String.format("%02x", b));
                }
                String passwordHashed = stringBuffer.toString();
                System.out.println(passwordHashed);
            }
        }

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:47:30+00:00Added an answer on June 4, 2026 at 4:47 pm

    You’ve pretty much nailed it. just took the hard/wrong way converting from char[] to String -> new String(password) is all you need. (hint, if you find yourself manually converting between bytes and chars, you’re probably doing it wrong).

    as a side note, exceptions are “thrown” for a reason. that makes it easy to skip the following code which shouldn’t be executed when the exception is thrown. by catching and turning the exceptions into “if” blocks, you make the code more complex than it needs to be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.