2

I am using "SHA-512" algorithm in Java to create hash with salt. The problem is in hashed string there is always a strange character which brakes line and when I print hashed string it is composed of two lines. But when I use SHA-256 it is shorter and it does not have a new line char.

So why I am getting two line when I use SHA-512?

EDIT: Sorry I did not mention that new line char is in encoded string. So my hashed string is like ".....hvRfXJwYDrnky/uUVzXnxz*\r\n*GFDJ+L3A......" characters "\r\n" causes problem..

2 Answers 2

2

Ah, then you need to set your Base64 line length to something longer. Base64 encoders usually have a max line length after which they add linebreaks.

sun.misc.BASE64Encoder is kind of an ugly encoder. I think you might be able to subclass it and override bytesPerLine. I would use org.apache.commons.codec.binary.Base64, you can specify the line length in the constructor.

Sign up to request clarification or add additional context in comments.

Comments

2

The problem is in hashed string there is always a strange character

That suggests that you're trying to convert the hash (which is binary data) into a string as if it were already encoded text, e.g.

// This is bad!
String text = new String(hashBytes);

A hash isn't encoded text - it's just raw binary data. To represent it as a string, you should use something like hex or base64. For base64 conversions, you can use something like this public domain library. For example:

// This won't lose data - it's reversible...
String base64 = Base64.encodeBytes(hashBytes);

Of course, if this isn't what your code is doing, then this answer is useless - but in that case, please post your code into your question.

12 Comments

sorry I did not mention that I am using BASE64Encoder to encode hashed string. So new line char is in base64 encoded string.
@huzeyfe: Ah, then you need to set your Base64 line length to something longer. Base64 encoders usually have a max line length after which they add linebreaks.
@huzeyfe: that's kind of an ugly encoder. I think you might be able to subclass it and override bytesPerLine. I would use org.apache.commons.codec.binary.Base64, you can specify the line length in the constructor.
@huzeyfe: Please show your code. If you're using base64 but getting a "strange character" then you're doing something wrong... the base64 encoder may split the data into two lines (it'll be 88 characters, I believe) but that's not the same as a "strange character". I would personally use the base64 library mentioned in my post.
@huzeyfe: How long do you think it'll take to download a zip file, extract the single Java file you need (it really is just one file, plus the docs which of course you don't have to bundle or anything) and use it? I suspect it'll be quicker than working with the existing ugly library.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.