Question

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..

Était-ce utile?

La solution

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.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top