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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:04:30+00:00 2026-05-22T15:04:30+00:00

I have this custom function to calculate MD5 hash, written in Java. I can’t

  • 0

I have this custom function to calculate MD5 hash, written in Java. I can’t change it. I need to translate it to JavaScript to use it on client side. I tried on my own but I can’t manage with JavaScript data types (expecially Java char[])… Any help is appreciated, thanks!

// codes array
char[] codes = new char[64];

// initialise
private void initCodes(){
  codes = new char[64];
  codes[0] = '$';
  int count = 0;
  for (char i='0';i<='9';i++){ count++; codes[count] = i; }
  for (char i='A';i<='Z';i++){ count++; codes[count] = i; }
  for (char i='a';i<='z';i++){ count++; codes[count] = i; }
  codes[63] = '£';
}

// custom MD5 algorithm
public String customMD5(String source) {
  initCodes();
  byte[] buf = new byte[source.length()];
  buf = source.getBytes();
  MessageDigest algorithm = null;
  try {
    algorithm = MessageDigest.getInstance("MD5");
  } catch(NoSuchAlgorithmException e){}
  algorithm.reset();
  algorithm.update(buf);
  byte[] digest = algorithm.digest();
  int len = digest.length;
  char[] encrypted = new char[len];
  for (int i=0;i<len;i++)
    encrypted[i] = codes[(int)(Math.floor((double)((digest[i]+128)/4)))];
  return new String(encrypted);
}
  • 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-22T15:04:31+00:00Added an answer on May 22, 2026 at 3:04 pm

    See this part here:

      MessageDigest algorithm = null;
      try{
         algorithm = MessageDigest.getInstance("MD5");
      }catch(NoSuchAlgorithmException e){}
    

    ? That’s where that stuff is accessing the MD5 code that’s built into the Java runtime. You’ll have to come up with your own implementation of MD5 there, which (to put it mildly) will be the tricky part.

    All that the posted Java code really does (on top of calling the runtime to do the actual hashing) is to map the resulting hash (part of it, anyway) through a character lookup table.

    edit — the lookup table built by that Java code is an array with “$”, the digits, the upper-case letters, the lower-case letters, and then (surprisingly) “£”. (The last character is surprising because it’s not an old-school 7-bit ASCII character code, but whatever.) In JavaScript, that’s:

    var codes = "$0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz£";
    

    The Java code then takes each 8-bit byte produced by the hash algorithm and looks up a code character by adding 128 to the byte and then dividing by 4. Java bytes are treated as signed values, so that has the effect of mapping every byte into the range 0 … 63. That value is then used as a lookup into the code array.

    Thus if you have a JavaScript MD5 facility that can give you back an array of numbers in the range -128 … 127 (that is, signed 8-bit values), you could translate the result through the code array like this:

    var digest = MagicJavaScriptMD5(source);
    var result = [];
    for (var i = 0; i < digest.length; ++i)
      result.push(codes.charAt(~~((digest[i] + 128) / 4)));
    var resultString = result.join('');
    

    EDIT by the OP:
    I take the liberty of posting here the right solution, that is highly derived from @Pointy’s one. It requires md5.js from http://pajhome.org.uk/crypt/md5/.

    /* MD5 in byte[] format */
    function byteArray_md5(s) {
            var output = [];
            var input = rstr_md5(str2rstr_utf8(s)); //here it uses md5.js
            for(var i = 0; i < input.length; i++)
                    output[i] = input.charCodeAt(i);
            return output;
    }
    /* MD5 with custom mapping.
     * It's a normal MD5 with a final char mapping of the hash.
     */
    function md5WithCustomMapping(source) {
        var codes = "$0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz£";
        var digest = byteArray_md5(source);
    
        var result = [];
        for (var i = 0; i < digest.length; ++i)
            result.push(
                    codes.charAt(
                            ~~( ( digest[i] + 128 * (digest[i]<128 ? 1 : -1) )/4 )
                            )
                        );
        return result.join('');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help writing custom functions for my casio fx-9860g I have done this
I have written a jQuery plugin which I want to call a custom function
I have this code: $('#custom-options label').click(function(){ $(this).removeClass('uncheck').addClass('checked'); }); What I'm aiming to do this
I have this question: Does implementing a custom MembershipProvider class needs you to implement
I have this legacy database for which I'm building a custom viewer using Linq
I have a custom class that implements that IComparable. This class is stored in
We have a custom service that writes to a DB (SQL 2005). This is
I have a custom class that implements ICollection , and this class is readonly,
I have a std::multimap where key is a custom class. Something like this: Class
I have a class which is marked with a custom attribute, like this: public

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.