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

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

hash - java object to Sha-256

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...