Skip to content

Instantly share code, notes, and snippets.

@hashbrowncipher
Created April 5, 2021 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hashbrowncipher/2c1e6a7ca5b34eb62f5c5acf47c02f00 to your computer and use it in GitHub Desktop.
Save hashbrowncipher/2c1e6a7ca5b34eb62f5c5acf47c02f00 to your computer and use it in GitHub Desktop.
josh@ubuntu:~/hasher$ dd if=/dev/zero bs=65536 count=16384 | ./xxHash/xxh128sum
16384+0 records in
16384+0 records out
16024760318c6298efd1151033ad2e9f stdin
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.23966 s, 4.5 GB/s
josh@ubuntu:~/hasher$ dd if=/dev/zero bs=65536 count=16384 | ./a.out
16384+0 records in
16384+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.210685 s, 5.1 GB/s
16024760318c6298efd1151033ad2e9f
// gcc -O3 -march=native -g -I ./xxHash hasher.c
#define _GNU_SOURCE
#include "xxh3.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void print_xxh128(unsigned char * hash) {
for(int i = 0; i < 16; i++) {
printf("%02x", hash[i]);
}
printf("\n");
}
void calcul_hash_streaming(int fd)
{
int pipes[2];
if(pipe(pipes) != 0) {
perror("pipe()");
exit(1);
}
size_t const bufferSize = 64 * 1024;
void* const buffer = malloc(bufferSize);
if (buffer==NULL) abort();
/* create a hash state */
XXH3_state_t state;
/* Initialize state with selected seed */
if (XXH3_128bits_reset(&state) == XXH_ERROR) abort();
size_t length;
while(1) {
// splice() has a shorter critical section than a simple read(), so
// pre-splicing reduces lock contention
length = splice(STDIN_FILENO, NULL, pipes[1], NULL, bufferSize, 0);
if(length == 0) {
break;
}
read(pipes[0], buffer, length);
if (XXH3_128bits_update(&state, buffer, length) == XXH_ERROR) abort();
}
/* Produce the final hash value */
XXH128_hash_t const hash = XXH3_128bits_digest(&state);
unsigned char digest[16];
XXH128_canonicalFromHash((XXH128_canonical_t *) digest, hash);
print_xxh128(digest);
free(buffer);
}
int main() {
calcul_hash_streaming(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment