Solved

byte array to hex

Posted on 2014-01-31
5
315 Views
Last Modified: 2014-02-12
Hi,

I am trying below example

http://www.avajava.com/tutorials/lessons/how-do-i-convert-a-byte-array-to-hex.html

I wonder what is the purpose and practical use od converting b yte aray to hex. And what is the use of md5 digest in this sceanario.
please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
  • 3
5 Comments
 
LVL 26

Expert Comment

by:dpearson
ID: 39825861
You often convert a byte array (binary data) into hex (character data) so it's easy to send it or use it somewhere that only accepts characters.

E.g. Say you wanted to take a series of binary values and post them onto this website so we can see them.  If you convert them to hex you get a string of simple characters.  If you posted the binary, they wouldn't display in a readable form.

Does that make sense?

MD5 hashing is a separate process.  Hashing is the process where you convert one value (the plaintext value) into a new value (the hashed value).  The important thing about hashing is that it's a 1-way process.  If I have a particular plaintext value it will always hash to the same value (for a given hash function like MD5).  BUT more than one plaintext value will hash to the same hashed value.  So we can have:

Hash("ABC") = 123
Hash("XYZ") = 52
Hash("KLM") = 123 <- it's the same as Hash("ABC")

Because of this hashing is frequently used when storing passwords.  The trick is that when you enter your password, it is hashed and the hashed value is sent to the server to store.
When you come back to the site, you type in your password, it is hashed to the same value as before and matches the one stored.

BUT because the hashed value is all that is stored in the database and hashed values aren't unique for a given plaintext value, there is no chance that somebody could work backwards from the hashed value to the original password.

Anyway that's a lot to digest.

Doug
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39828668
Anyway that's a lot to digest.
Doug, was the pun intended? ;)
0
 
LVL 26

Expert Comment

by:dpearson
ID: 39828816
Anyway that's a lot to digest.
Doug, was the pun intended? ;)

Haha - um no, not at all...
0
 
LVL 7

Author Comment

by:gudii9
ID: 39842902
The important thing about hashing is that it's a 1-way process.  If I have a particular plaintext value it will always hash to the same value (for a given hash function like MD5).  BUT more than one plaintext value will hash to the same hashed value.  So we can have:

Hash("ABC") = 123
Hash("XYZ") = 52
Hash("KLM") = 123 <- it's the same as Hash("ABC")

How ABC and KLM both has 123 as Hash value. can you please elaborate on that.
0
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 39843034
How ABC and KLM both has 123 as Hash value. can you please elaborate on that.

Open in new window


It's the nature of hash functions that they can do this.

Say for instance a simple hash function is:

int hash(String str) {
   return str.chatAt(0) ;
}

This will sort all strings into the same bucket that start with the same letter.  (This is sort of the algorithm you use in a real dictionary - thumb through the top to find the words starting with the letter you want, then proceed further in).

This is a perfectly legal hash function just not a very good one.

For that hash function you'd get that:

hash("ABC") = 65 ;
hash("DEF") = 68 ;
hash("ADE") = 65 ; // Same as for ABC because they start with the same letter

See how hash("ABC") == hash("ADE").

That's the way hashing is meant to work:
a) A given string always hashes to the same value
    hash("ABC") will always return 65.  It can't vary over time.

b) Multiple strings can hash to the same value
    hash("ABC") = hash("ADE") is fine.

Doug
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now