SHA-1: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 12: Line 12:
==Git==
==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''>
  [[git hash-object]] <''file-name''>

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