Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...