snapkitty
quantum-computing
python
File size: 1,521 Bytes
4953a87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * PROPRIETARY AND CONFIDENTIAL -- PRIOR ART SEALED
 * Copyright (C) 2026 SNAPKITTYWEST / SnapKitty (Jessica).
 * SHA-256 C shim for Idris 2 FFI.
 * Links against OpenSSL libcrypto.
 */

#include <openssl/sha.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
 * Read a file and return its SHA-256 digest as a 64-char hex string.
 * Caller must free the returned string.
 * Returns NULL on error.
 */
char* idris2_sha256_file(const char* path) {
    FILE* f = fopen(path, "rb");
    if (!f) return NULL;

    SHA256_CTX ctx;
    SHA256_Init(&ctx);

    unsigned char buf[65536];
    size_t n;
    while ((n = fread(buf, 1, sizeof(buf), f)) > 0) {
        SHA256_Update(&ctx, buf, n);
    }
    fclose(f);

    unsigned char digest[SHA256_DIGEST_LENGTH];
    SHA256_Final(digest, &ctx);

    char* hex = malloc(65);
    if (!hex) return NULL;
    for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
        sprintf(hex + i * 2, "%02x", digest[i]);
    }
    hex[64] = '\0';
    return hex;
}

/*
 * SHA-256 of a raw string (for sealing entry metadata).
 * Caller must free the returned string.
 */
char* idris2_sha256_string(const char* s) {
    SHA256_CTX ctx;
    SHA256_Init(&ctx);
    SHA256_Update(&ctx, s, strlen(s));

    unsigned char digest[SHA256_DIGEST_LENGTH];
    SHA256_Final(digest, &ctx);

    char* hex = malloc(65);
    if (!hex) return NULL;
    for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
        sprintf(hex + i * 2, "%02x", digest[i]);
    }
    hex[64] = '\0';
    return hex;
}