Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "openssl/sha.h"
- #include <time.h>
- #include <stdio.h>
- #include <string.h>
- static int estimate_entropy(long l) {
- int bits = 1; /* for the sign bit */
- if (l < 0) l = -l;
- while (l > 0) {
- ++bits;
- l >>= 1;
- }
- return bits;
- }
- int main() {
- clockid_t clockid = CLOCK_REALTIME;
- struct timespec ts;
- if (clock_getres(clockid, &ts) != 0) {
- perror("clock_getres");
- return 1;
- }
- printf("Clock resolution: %ld.%09ld\n", (long) ts.tv_sec, (long) ts.tv_nsec);
- unsigned char hash[SHA256_DIGEST_LENGTH];
- memset(hash, 0, sizeof(hash));
- #define N 50 /* number of samples */
- struct timespec samples[N];
- memset(samples, 0, sizeof(samples));
- for (int i = 0; i < N; ++i) {
- clock_gettime(clockid, &samples[i]);
- SHA256_CTX ctx;
- SHA256_Init(&ctx);
- SHA256_Update(&ctx, &hash, sizeof(hash));
- SHA256_Update(&ctx, &samples[i], sizeof(samples[i]));
- SHA256_Final(hash, &ctx);
- }
- printf("Hash: ");
- for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) printf("%02x", hash[i]);
- printf("\n");
- printf("Deltas (ns):");
- long deltas[N - 1];
- for (int i = 0; i < N - 1; ++i) {
- deltas[i] =
- (samples[i + 1].tv_sec - samples[i].tv_sec)*1000000000L
- + (samples[i + 1].tv_nsec - samples[i].tv_nsec);
- printf(" %4ld", deltas[i]);
- }
- printf("\n");
- long entropy = 0;
- printf("Deltas of deltas: ");
- for (int i = 0; i < N - 2; ++i) {
- long dd = deltas[i + 1] - deltas[i];
- printf(" %4ld", dd);
- entropy += estimate_entropy(dd);
- }
- printf("\n");
- printf("Maximum entropy: %lld\n", entropy);
- }
Advertisement
Add Comment
Please, Sign In to add comment