Guest User

Untitled

a guest
Sep 22nd, 2026
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. #include "openssl/sha.h"
  2.  
  3. #include <time.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. static int estimate_entropy(long l) {
  8.     int bits = 1; /* for the sign bit */
  9.     if (l < 0) l = -l;
  10.     while (l > 0) {
  11.         ++bits;
  12.         l >>= 1;
  13.     }
  14.     return bits;
  15. }
  16.  
  17. int main() {
  18.     clockid_t clockid = CLOCK_REALTIME;
  19.  
  20.     struct timespec ts;
  21.     if (clock_getres(clockid, &ts) != 0) {
  22.         perror("clock_getres");
  23.         return 1;
  24.     }
  25.     printf("Clock resolution: %ld.%09ld\n", (long) ts.tv_sec, (long) ts.tv_nsec);
  26.    
  27.     unsigned char hash[SHA256_DIGEST_LENGTH];
  28.     memset(hash, 0, sizeof(hash));
  29.  
  30.     #define N 50  /* number of samples */
  31.     struct timespec samples[N];
  32.     memset(samples, 0, sizeof(samples));
  33.     for (int i = 0; i < N; ++i) {
  34.         clock_gettime(clockid, &samples[i]);
  35.         SHA256_CTX ctx;
  36.         SHA256_Init(&ctx);
  37.         SHA256_Update(&ctx, &hash, sizeof(hash));
  38.         SHA256_Update(&ctx, &samples[i], sizeof(samples[i]));
  39.         SHA256_Final(hash, &ctx);
  40.     }
  41.  
  42.     printf("Hash: ");
  43.     for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) printf("%02x", hash[i]);
  44.     printf("\n");
  45.  
  46.     printf("Deltas (ns):");
  47.     long deltas[N - 1];
  48.     for (int i = 0; i < N - 1; ++i) {
  49.         deltas[i] =
  50.             (samples[i + 1].tv_sec - samples[i].tv_sec)*1000000000L
  51.             + (samples[i + 1].tv_nsec - samples[i].tv_nsec);
  52.         printf(" %4ld", deltas[i]);
  53.     }
  54.     printf("\n");
  55.     long entropy = 0;
  56.     printf("Deltas of deltas: ");
  57.     for (int i = 0; i < N - 2; ++i) {
  58.         long dd = deltas[i + 1] - deltas[i];
  59.         printf(" %4ld", dd);
  60.         entropy += estimate_entropy(dd);
  61.     }
  62.     printf("\n");
  63.     printf("Maximum entropy: %lld\n", entropy);
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment