0

I have a code like

message = "abc".encode()
messageDigest = java.security.MessageDigest.getInstance("SHA-256")
messageDigest.update(message)
hashdata = messageDigest.digest()

Here whhen I print hashdata I get <<java class 'byte[]'> at 0x7f1ee005df60>

But I need its value. How can I get its value ?

1
  • Use like this.. String hashdata = new String(messageDigest.digest()); Commented Jan 6, 2020 at 8:10

2 Answers 2

1

Currently we do not support the __bytes__ operator for a Java byte array, but it is easy to add with a customizer.

import jpype
from jpype import java

# Add the customizer before starting the JVM
@jpype.JImplementationFor('byte[]')
class ByteConverter(object):
    def __bytes__(self):
        return bytes(self[:])

# Now start the JVM
jpype.startJVM(convertStrings=False)

# Perform Java operations
message = "abc".encode()
messageDigest = java.security.MessageDigest.getInstance("SHA-256")
messageDigest.update(message)
hashdata = messageDigest.digest()

# We got back a Java byte[] and we would like a Python bytes
print(bytes(hashdata))
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

for (int i = 0; i < hash.length; i++) {
    if ((0xff & hash[i]) < 0x10) {
        hexString.append("0"
                + Integer.toHexString((0xFF & hash[i])));
    } else {
        hexString.append(Integer.toHexString(0xFF & hash[i]));
    }
}

Comments

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.