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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:45:49+00:00 2026-06-13T22:45:49+00:00

I made a hash algorithm that uses MD5 for some low-security key generation. Basically,

  • 0

I made a hash algorithm that uses MD5 for some low-security key generation. Basically, it takes the characters of a String and sums their indexed products, then takes the modulo of a random number, before MD5-ing that. In Java:

BigInteger bi = BigInteger.ZERO;
char[] array = input.toCharArray();
for (int i = 0; i < array.length; i++) {
    bi = bi.add(BigInteger.valueOf(i + 1).multiply(
            BigInteger.valueOf(array[i])));
}
final int moduloOperator = 52665; // random constant
final byte[] moduloResult = bi.remainder(
        BigInteger.valueOf(moduloOperator)).toByteArray();
MessageDigest md;
try {
    md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException nsae) {
    nsae.printStackTrace();
    return null;
}
md.update(moduloResult);
return new BigInteger(1, md.digest()).toString().substring(0, 7);

I have the substring at the end because it needs to be easily readable.

At first glance, it works as intended: different inputs give different outputs, but the result is consistent across runs.

However, when playing with it a bit, I noticed the following:

hash("")        = "1963546"
hash("1963546") = "1322048"
hash("1322048") = "2101764"
hash("2101764") = "3234562"

Looks fine so far. Suitably random. But then:

hash("3234562") = "3234562"
hash("3234562") = "3234562" [etc.]

This dumbfounded me. I would guess that there’s about a one in ten million chance that the hash of a 7-digit number is itself. Did this really happen on only the fifth iteration, or is there something wrong with my setup? More importantly, could there be any other similar errors that could have a serious impact on my hash?

Thanks.

  • 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-13T22:45:50+00:00Added an answer on June 13, 2026 at 10:45 pm

    The “random” part of your code is doing more harm than good.

    First, the code adds together several uncorrelated numbers:

    for (int i = 0; i < array.length; i++) {
    bi = bi.add(BigInteger.valueOf(i + 1).multiply(
            BigInteger.valueOf(array[i])));
    }
    

    Let’s see the result of this for “2101764” and “3234562”. I’ll use Python for brevity.

    In [0]: sum((i+1)*int(digit) for (i, digit) in enumerate("3234562"))
    Out[0]: 107
    
    In [1]: sum((i+1)*int(digit) for (i, digit) in enumerate("2101764"))
    Out[1]: 107
    

    Well, there’s your problem.

    Remember the Central Limit Theorem? The sum of random numbers is much more predictable than the individual numbers themselves. Back of the envelope, for a 7 digit input the sum will have a distribution with a variance of 13.16 and mean of 115.5. It would be safe to infer at least of all 60% of sums will be within a 50 number range, 95% of sums within a 100 number range, and all sums within a 189 number range — if anything, I think this is generous about the entropy of the sum.

    After destroying information through addition, the algorithm takes the sum modulo 52665. There are only 52665 possible numbers modulo 52665, so this code can only ever produce 52665 hashes in the best of circumstances.

    And…There’s no reason to do any of this! Random code does not make random numbers. Making a good hash function is hard. You’re not going to improve on a hash by hacking up some code to slice and dice things. On the contrary, you are likely destroy sources of randomness. If you want a random hash, use one that someone else has written.

    Say, for example, MD5!

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

Sidebar

Related Questions

I was wrestling with some Perl that uses hash references. In the end it
Made a custom obj called Item with some string fields and one float. .h
I have made a dropdown menu that uses tabs to display the specific content
I have a template class that I have made called hash . My template
I've made a general hash up of the following function. Basically linking this function
This is sample PHP code I made to hash a string. Does this method
I made an iphone app to capture image from camera and to set that
I made a Web service in which I have a function to count some
I made an application that passes trough an XML file and extracts the entries
I made a little PHP script that checks if an email is valid. 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.