SHA-1: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 5: Line 5:


=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.
!!!Compute the SHA1 Hash of a File
The SHA1 hash can be calculated with [git hash-object].
{{{
git hash-object <file-name>
}}}
!!!Programmatically Calculate String SHA1 in 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);
        //noinspection UnnecessaryLocalVariable
        byte[] hash = sha1.digest();
        return hash;
}
}}}

Revision as of 08:14, 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.


!!!Compute the SHA1 Hash of a File

The SHA1 hash can be calculated with [git hash-object].


{{{ git hash-object <file-name> }}}


!!!Programmatically Calculate String SHA1 in 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);
       //noinspection UnnecessaryLocalVariable
       byte[] hash = sha1.digest();
       return hash;

} }}}