SHA-1: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 8: Line 8:
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.  
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=


!!!Compute the SHA1 Hash of a File
==Git==


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


[[git hash-object]] <''file-name''>


==Java==


{{{
<syntaxhighlight lang='java'>
git hash-object <file-name>
public static byte[] toSha1(String s) throws Exception {
}}}


    MessageDigest sha1 = MessageDigest.getInstance("SHA-1");


!!!Programmatically Calculate String SHA1 in Java
    sha1.reset();


{{{
    byte[] bytes = s.getBytes("utf8");
public static byte[] toSha1(String s) throws Exception
{
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");


        sha1.reset();
    sha1.update(bytes);


        byte[] bytes = s.getBytes("utf8");
    byte[] hash = sha1.digest();


        sha1.update(bytes);
    return hash;
 
        //noinspection UnnecessaryLocalVariable
        byte[] hash = sha1.digest();
 
        return hash;
}
}
}}}
</syntaxhighlight>

Revision as of 08:16, 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;
}