曹耘豪的博客

MD5,sha1,sha-256等加密方式

java

1
2
3
4
5
6
FileInputStream fileInputStream = new FileInputStream(path);
byte[] bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes);
MessageDigest digest = MessageDigest.getInstance("md5"); // md5, sha1, sha-256
digest.update(bytes);
String md5Code = new BigInteger(1, digest.digest()).toString(16);

python

1
2
3
4
5
6
7
import hashlib

with open(file_path, 'rb') as f:
data = f.read()
hashlib.md5(data).hexdigest()
hashlib.sha1(data).hexdigest()
hashlib.sha256(data).hexdigest()