简体   繁体   中英

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());
}

MessageDigest only works with bytes. It doesn't even accept String s. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM