> ## Documentation Index
> Fetch the complete documentation index at: https://trustos.telefonicatech.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Hashing Method

Cert API **never stores any document or file** used during the process. The document is double hashed and the result is registered on Blockchain as an evidence.

In terms of using the double hashing method, you can try any of the following methods or replicate the code in your preferred programming language.

To validate we obtain same result in both sides (API side and yours), you can try to double hash this file: `filetohash.pdf` to obtain the following double hash result.

`Double hash: 2c124ab4054cfd34c00ac8fbde3f17d0fb6d512b26396626558d06b594061b4a`

To help the hash replication process, you can check first hash and second hash are aligned:

`First hash: dc8144e66121409a4e4a97e02670e39a3bd40d86911a368ec1c6760671f375ba`

`Double hash: 2c124ab4054cfd34c00ac8fbde3f17d0fb6d512b26396626558d06b594061b4a`

Download the `filetohash.pdf` file here 👇

<a href="/public/files/fileToHash.pdf" download>📁 filetohash.pdf</a>

Check out the double hash method in any of these languages:

<CodeGroup>
  ```javascript Javascript theme={null}
  const fs = require("fs");
  const crypto = require("crypto-js");

  function hash(content) {
  	try {
  		// First hash (WordArray hash)
  		const wordArray = crypto.lib.WordArray.create(content);
  		const firstHash = crypto.SHA256(wordArray);
  		const firstHashHex = firstHash.toString(crypto.enc.Hex);
  		console.log("First hash:" + firstHashHex);

  		// Second hash (WordArray hash)
  		const firstHashWordArray = crypto.enc.Hex.parse(firstHashHex);
  		const secondHash = crypto.SHA256(firstHashWordArray);
  		const secondHashHex = secondHash.toString(crypto.enc.Hex);
  		console.log("Double hash:" + secondHashHex);

  		// Returns the double hash
  		return secondHashHex;
  	} catch (err) {
  		console.log(`Error hashing the file content: ${err}`);
  		throw err;
  	}
  }
  const filePath = "./filetohash.pdf";
  const fileContent = fs.readFileSync(filePath, {});
  hash(fileContent);
  ```

  ```python Python theme={null}
  import hashlib

  def hash(data):
  	sha256 = hashlib.sha256()
  	sha256.update(data)
  	return hashlib.sha256(sha256.digest()).hexdigest()

  file_path = "./filetohash.pdf"
  with open(file_path, "rb") as f:
  	file_content = f.read()
  hash(file_content)
  ```

  ```java Java theme={null}
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.security.MessageDigest;
  import java.security.NoSuchAlgorithmException;

  public class JavaExample {
  	public static String hashFile(File file) throws NoSuchAlgorithmException, IOException {
  		// create a MessageDigest object using SHA-256 algorithm
  		MessageDigest digest1 = MessageDigest.getInstance("SHA-256");

  		// read the file contents into a byte array
  		FileInputStream fis = new FileInputStream(file);
  		byte[] buffer = new byte[8192];
  		int len;
  		while ((len = fis.read(buffer)) > 0) {
  			digest1.update(buffer, 0, len);
  		}
  		fis.close();

  		// compute the hash value of the file contents
  		byte[] fileHash = digest1.digest();

  		// create a new MessageDigest object for the second hash
  		MessageDigest digest2 = MessageDigest.getInstance("SHA-256");

  		// update the second hash with the first hash value
  		digest2.update(fileHash);

  		// compute the second hash value
  		byte[] doubleHash = digest2.digest();

  		// convert the byte arrays to hexadecimal strings
  		String fileHashHex = bytesToHex(fileHash);
  		String doubleHashHex = bytesToHex(doubleHash);

  		// print the hash values and return the double hash value
  		System.out.println("First hash: " + fileHashHex);
  		System.out.println("Double hash: " + doubleHashHex);
  		return doubleHashHex;
  	}

  	private static String bytesToHex(byte[] bytes) {
  		StringBuilder sb = new StringBuilder();
  		for (byte b : bytes) {
  			sb.append(String.format("%02x", b));
  		}
  		return sb.toString();
  	}

  	public static void main(String[] args) throws Exception {
  		File file = new File("./filetohash.pdf");
  		hashFile(file);
  	}
  }
  ```

  ```go Go theme={null}
  package main

  import (
  	"crypto/sha256"
  	"encoding/hex"
  	"fmt"
  	"io/ioutil"
  )

  func hash(content []byte) (string, error) {

  	// First hash (WordArray hash)
  	h1 := sha256.Sum256(content)
  	h1Hex := hex.EncodeToString(h1[:])
  	fmt.Printf("First Hash (hex): %s\n", h1Hex)

  	// Second hash (WordArray hash)
  	h2 := sha256.Sum256(h1[:])
  	h2Hex := hex.EncodeToString(h2[:])
  	fmt.Printf("Double Hash (hex): %s\n", h2Hex)

  	// Return the double hash
  	return h2Hex, nil

  }

  func main() {
  	// Read file contents
  	filename := "filetohash.pdf"
  	data, err := ioutil.ReadFile(filename)
  	if err != nil {
  		fmt.Printf("Error reading file: %s", err)
  		return
  	}
  	// Call double hash method
  	hash(data)
  }
  ```

  ```c C theme={null}
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <openssl/sha.h>

  char *hash(unsigned char *data, size_t len)
  {
  	unsigned char hash[SHA256_DIGEST_LENGTH];
  	SHA256_CTX sha256;
  	SHA256_Init(&sha256);
  	SHA256_Update(&sha256, data, len);
  	SHA256_Final(hash, &sha256);

  	unsigned char hash2[SHA256_DIGEST_LENGTH];
  	SHA256_Init(&sha256);
  	SHA256_Update(&sha256, hash, SHA256_DIGEST_LENGTH);
  	SHA256_Final(hash2, &sha256);

  	char *output = (char *)malloc((SHA256_DIGEST_LENGTH * 2 + 1) * sizeof(char));
  	for (int i = 0; i <CodeGroup SHA256_DIGEST_LENGTH; i++)
  	{
  		sprintf(&output[i * 2], "%02x", (unsigned int)hash2[i]);
  	}
  	return output;
  }

  int main()
  {
  	char *file_path = "./filetohash.pdf";
  	FILE *f = fopen(file_path, "rb");
  	if (!f)
  	{
  		printf("Error opening file\n");
  		return 1;
  	}
  	fseek(f, 0, SEEK_END);
  	long file_size = ftell(f);
  	fseek(f, 0, SEEK_SET);
  	unsigned char *file_content = (unsigned char *)malloc(file_size * sizeof(unsigned char));
  	fread(file_content, sizeof(unsigned char), file_size, f);
  	fclose(f);

  	char *hash_value = hash(file_content, file_size);
  	printf("Hash value: %s\n", hash_value);
  	free(hash_value);
  	free(file_content);
  	return 0;
  }
  ```
</CodeGroup>
