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

  • SEARCH
  • Home
  • 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 7834909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:23:33+00:00 2026-06-02T13:23:33+00:00

Background: I have an iterative hash algorithm I need to compute from a Python

  • 0

Background: I have an iterative hash algorithm I need to compute from a Python script and a Java web application.

Psuedo code:

hash = sha256(raw)
for x=1 to 64000 hash = sha256(hash)

where hash is a byte array of length 32, and not a hex string of length 64.

The reason I want to keep it in bytes is because, though Python can convert to hex string in between each iteration and keep the processing time under a second, Java takes 3 seconds for the String overhead.

So, the Java code looks like this:

// hash one time...
byte[] result = sha256(raw.getBytes("UTF-8"));

// then hash 64k-1 more times
for (int x = 0; x < 64000-1; x++) {
  result = sha256(result);
}

// hex encode and print result
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
for (int i=0; i<buf.length; i++) {
  formatter.format("%02x", buf[i]);
}
System.out.println(sb.toString());

And the Python code looks like this:

import hashlib

# hash 1 time...
hasher = hashlib.sha256()
hasher.update(raw)
digest = hasher.digest()

# then hash 64k-1 times
for x in range (0, 64000-1):
  # expect digest is bytes and not hex string
  hasher.update(digest) 
  digest = hasher.digest()
print digest.encode("hex")

The Python result calculated the hash on the hex representation of the first digest (String), rather than the raw digest bytes. So, I get varying outputs.

  • 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-02T13:23:34+00:00Added an answer on June 2, 2026 at 1:23 pm

    Method .update of hasher appends argument to previous text (Python docs). Instead you should create new hasher each time you want to compute digest.

    import hashlib
    
    # hash 1 time...
    digest = hashlib.sha256(raw).digest()
    
    # then hash 64k-1 times
    for x in range(0, 64000-1):
      digest = hashlib.sha256(digest).digest()
    print digest.encode("hex")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: we have an application that generates reports from HTML (that may or may
I am learning Python (I have a C/C++ background). I need to write something
I have a strong python background and a total newbie with Java, so I
Background I have made a Web Service in Visual Studio, and I'm trying to
Background I have an application written in native C++ over the course of several
I have a rather simple C++-problem, but coming from a C-background I am not
I have not used multi threading much for asp.net. I have a web application
I have a script that asynchronously loads image src from XML file. The sources
Background I have a service that is connecting to Microsoft CRM 2011, and updating
Background: I have a little video playing app with a UI inspired by the

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.