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 7017803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:57:43+00:00 2026-05-27T22:57:43+00:00

I am using the following code to hash an incoming string, in expecting that

  • 0

I am using the following code to hash an incoming string, in expecting that same thing applied to the method multiple times will always get the same results. The scenario will be for password hashing and later verification. But it doesn’t seem to work – I got two different blobs for the same input string. Is there anything wrong or missing with my code?

public synchronized String encrypt(String token) {
    try {
        MessageDigest sha = MessageDigest.getInstance("SHA");
        sha.reset();
        sha.update(token.getBytes("UTF-8"));
        byte[] raw = sha.digest();
        System.out.println("raw = " + raw.toString());
        String hash = Base64.encodeBase64(raw).toString();
        return hash;
    } catch (Exception e) {
    }

    return token;
}
  • 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-05-27T22:57:44+00:00Added an answer on May 27, 2026 at 10:57 pm

    You haven’t really given enough information, but I suspect you’re being distracted by this:

    System.out.println("raw = " + raw.toString());
    

    That’s going to print out something like [B@30a4effe which has nothing to do with the data in the byte array. You should print out hash instead – which should be the same for all calls, if your token is genuinely the same.

    (As noted by Dan, your method is inappropriately named: hashing isn’t encryption. Also, please don’t catch Exception or just swallow exceptions like this. It seems pretty odd to just return token on failure, too.)

    EDIT: As noted, I’ve assumed that Base64.encode actually returns a String, which it may not. I’d recommend this base64 implementation which is public domain and has a sensible API – the encoding calls return a String, which is entirely appropriate. Of course, you then don’t need the explicit toString() call as well…

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

Sidebar

Related Questions

In Objective C I've been using the following code to hash a string: -(NSString
Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something
I am using following code to convert string to sha1 string but i am
I'm using the following code to encode a simple hash use JSON; my $name
Im using the following code within a method called a_level: if(@program.theme==MyHelper::TemplateConstants::DEFAULT_LAYOUT) style = 'border:
Hi I have some strings generated using the following code: private static string CalcHashCode(byte[]
I'm reading an InputStream to a String using the following code. public String convertStreamToString(InputStream
I am using following code in rowdatabound function. Protected Sub gvwMileStone_RowDataBound(ByVal sender As System.Object,
I am using following code to check whether a check box on my website
I am using following code to design my home page. The output (as shown

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.