Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to use Hash Function. I understand that I can digest string or bytes. But, what is the input value when I use Object O for the input?

Object O has two int values, and toString() function

public Key(int arg1, int arg2) {
    super();
    this.a=arg1;
    this.b=arg2;
}


try{
    MessageDigest v1 = MessageDigest.getInstance("SHA-256");
    O.update(v1);
    v2_1 = BytesToHex(v1.digest());
}
question from:https://stackoverflow.com/questions/65877482/java-object-to-sha-256

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
657 views
Welcome To Ask or Share your Answers For Others

1 Answer

MessageDigest only works with bytes. It doesn't even accept Strings. Just bytes.

These are the available update methods. You can see that all four of them only deal with bytes:

void update?(byte input);
void update?(byte[] input);
void update?(byte[] input, int offset, int len);
void update?(ByteBuffer input);

It's up to you to serialize objects into bytes using the procedure of your choice. MessageDigest doesn't handle it for you, nor does it have any convenience methods to assist in the process. It doesn't deal with anything except raw bytes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...