0

it there any way to create a md5 for given string, that md5 should have only specified characters say only alphanumeric, also the length of md5 should be coder defined using java.

Example:

suppose text of this question is a string to make a md5 and it has "//:\ #$%^&*!()_-=+|" these characters at the start and end of this test i need to have a md5 like.

expected OUTPUT:

sdki544k54353453 (a 16 length string with only alphanumeric )

Badr
  • 10,384
  • 15
  • 70
  • 104
  • Could you include any example inputs and outputs? – FThompson Nov 27 '12 at 07:04
  • possible duplicate of [Getting a File's MD5 Checksum in Java](http://stackoverflow.com/questions/304268/getting-a-files-md5-checksum-in-java) – Dante May Code Nov 27 '12 at 07:06
  • i look at this post but my question is how can i find a md5 with only alphanumeric and with specified length also, say 16 in my case – Badr Nov 27 '12 at 07:11

1 Answers1

3

it there any way to create a md5 for given string

First problem: you need to define which encoding you want to use to convert the string to bytes. MD5 works on binary data, not text. So you need to convert your string to bytes, then hash it, which will give you 16 bytes back as the hash.

that md5 should have only specified characters say only alphanumeric

Well, that's most easily done in hex, or base32 if you want to save a bit of space.

also the length of md5 should be coder defined using java

The size of the binary hashed data is fixed - MD5 always produces 128 bits (16 bytes). If you want to reduce the length, you'd potentially need to truncate the data (which makes the hash less effective). Of course, if your desired length is longer than it needs to be, you just need to pad it.

16 bytes would be encoded in hex as 32 characters, or in base32 as 26 characters. Base64 would only need 22 characters, but that requires at least two non-alphanumeric characters. (Digits + a-z + A-Z gives 62 characters; base64 needs 64 - usually plus a padding character, but that could be omitted if you know the length beforehand.)

If you could explain your requirements in more detail, we may be able to help more - but fundamentally MD5 is a fixed-size hash.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194