SHA-1

From NovaOrdis Knowledge Base
Revision as of 08:20, 6 May 2018 by Ovidiu (talk | contribs) (→‎Internal)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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