SHA-1: Difference between revisions
Jump to navigation
Jump to search
(3 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
* [[Cryptography#SHA-1|Cryptography]] | * [[Cryptography#SHA-1|Cryptography]] | ||
* [[MD5#Overview|MD5]] | * [[MD5#Overview|MD5]] | ||
* [[Git_Concepts#SHA1.2C_Hash_Code.2C_Object_ID|Git Concepts]] | |||
=Overview= | =Overview= | ||
SHA1 always computes the same ID for identical content. Thus the SHA1 hash ID of a file is a globally unique identifier. Files or blobs of arbitrary size can be compared for equality across the Internet by merely comparing their SHA1 identifier. The hash function is also called ''digest''. The hash function provides an efficient way to compare two objects, even two very large and complex data structures, without transmitting them in full. SHA1 values are 160-bit (20 bytes) values that are usually represented as a 40-digit hexadecimal number. | |||
=Computing SHA-1= | |||
==Git== | |||
The SHA1 hash can be calculated with [[git hash-object]]: | |||
[[git hash-object]] <''file-name''> | |||
==Java== | |||
<syntaxhighlight lang='java'> | |||
public static byte[] toSha1(String s) throws Exception { | |||
MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); | |||
sha1.reset(); | |||
byte[] bytes = s.getBytes("utf8"); | |||
sha1.update(bytes); | |||
byte[] hash = sha1.digest(); | |||
return hash; | |||
} | |||
</syntaxhighlight> |
Latest revision as of 08:20, 6 May 2018
Internal
Overview
SHA1 always computes the same ID for identical content. Thus the SHA1 hash ID of a file is a globally unique identifier. Files or blobs of arbitrary size can be compared for equality across the Internet by merely comparing their SHA1 identifier. The hash function is also called digest. The hash function provides an efficient way to compare two objects, even two very large and complex data structures, without transmitting them in full. SHA1 values are 160-bit (20 bytes) values that are usually represented as a 40-digit hexadecimal number.
Computing SHA-1
Git
The SHA1 hash can be calculated with git hash-object:
git hash-object <file-name>
Java
public static byte[] toSha1(String s) throws Exception {
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
sha1.reset();
byte[] bytes = s.getBytes("utf8");
sha1.update(bytes);
byte[] hash = sha1.digest();
return hash;
}