|
1 | 1 | #define rustg_hash_string(algorithm, text) RUSTG_CALL(RUST_G, "hash_string")(algorithm, text) |
2 | 2 | #define rustg_hash_file(algorithm, fname) RUSTG_CALL(RUST_G, "hash_file")(algorithm, fname) |
3 | | -#define rustg_hash_generate_totp(seed) RUSTG_CALL(RUST_G, "generate_totp")(seed) |
4 | | -#define rustg_hash_generate_totp_tolerance(seed, tolerance) RUSTG_CALL(RUST_G, "generate_totp_tolerance")(seed, tolerance) |
| 3 | + |
| 4 | +/// Supported algorithms: RUSTG_HASH_SHA1, RUSTG_HASH_SHA256, RUSTG_HASH_SHA512 |
| 5 | +/// Seed must be between 10 bytes to 64 bytes (padded or unpadded) of base32. 20 bytes is recommended. Use a CSPRNG. |
| 6 | +/// Refresh rate is fixed at 30sec and digit count is fixed at 6 |
| 7 | +#define rustg_hash_generate_totp(algorithm, seed) RUSTG_CALL(RUST_G, "generate_totp")(algorithm, seed) |
| 8 | +/// Supported algorithms: RUSTG_HASH_SHA1, RUSTG_HASH_SHA256, RUSTG_HASH_SHA512 |
| 9 | +/// Seed must be between 10 bytes to 64 bytes (padded or unpadded) of base32. 20 bytes is recommended. Use a CSPRNG. |
| 10 | +/// Refresh rate is fixed at 30sec and digit count is fixed at 6 |
| 11 | +/// Tolerance is the number of codes +-30sec from the current one that are allowed. |
| 12 | +#define rustg_hash_generate_totp_tolerance(algorithm, seed, tolerance) RUSTG_CALL(RUST_G, "generate_totp_tolerance")(algorithm, seed, tolerance) |
| 13 | + |
| 14 | +/// Creates a cryptographically-secure pseudorandom number generator using the OS-level PRNG as a seed |
| 15 | +/// n_bytes is the number of bytes provided to the RNG, the length of the string output varies by format |
| 16 | +/// The output string length and characters contained in each format is as follows: |
| 17 | +/// RUSTG_RNG_FORMAT_HEX: n_bytes * 2, [a-z0-9] |
| 18 | +/// RUSTG_RNG_FORMAT_ALPHANUMERIC: n_bytes, [A-Za-z0-9] |
| 19 | +/// RUSTG_RNG_FORMAT_BASE32: ceil(n_bytes / 5 * 8) [A-Z2-7] |
| 20 | +/// RUSTG_RNG_FORMAT_BASE32_PADDED: ceil(n_bytes / 5) * 8 [A-Z2-7=] |
| 21 | +/// RUSTG_RNG_FORMAT_BASE64: 4 * ceil(n_bytes/3), [A-Za-z0-9+/=] |
| 22 | +/// Outputs "ERROR: [reason]" if the format string provided is invalid, or n_bytes is not a positive non-zero integer |
| 23 | +#define rustg_csprng_chacha20(format, n_bytes) RUSTG_CALL(RUST_G, "csprng_chacha20")(format, "[n_bytes]") |
| 24 | + |
| 25 | +/// Creates a seeded pseudorandom number generator using the SHA256 hash output bytes of the seed string |
| 26 | +/// Note that this function is NOT suitable for use in cryptography and is intended for high-quality **predictable** RNG |
| 27 | +/// Use rustg_csprng_chacha20 for a cryptographically-secure PRNG. |
| 28 | +/// n_bytes is the number of bytes provided to the RNG, the length of the string output varies by format |
| 29 | +/// The output string length and characters contained in each format is as follows: |
| 30 | +/// RUSTG_RNG_FORMAT_HEX: n_bytes * 2, [a-z0-9] |
| 31 | +/// RUSTG_RNG_FORMAT_ALPHANUMERIC: n_bytes, [A-Za-z0-9] |
| 32 | +/// RUSTG_RNG_FORMAT_BASE32: ceil(n_bytes / 5 * 8) [A-Z2-7] |
| 33 | +/// RUSTG_RNG_FORMAT_BASE32_PADDED: ceil(n_bytes / 5) * 8 [A-Z2-7=] |
| 34 | +/// RUSTG_RNG_FORMAT_BASE64: 4 * ceil(n_bytes/3), [A-Za-z0-9+/=] |
| 35 | +/// Outputs "ERROR: [reason]" if the format string provided is invalid, or n_bytes is not a positive non-zero integer |
| 36 | +#define rustg_prng_chacha20_seeded(format, n_bytes, seed) RUSTG_CALL(RUST_G, "prng_chacha20_seeded")(format, "[n_bytes]", seed) |
| 37 | + |
| 38 | +#define RUSTG_RNG_FORMAT_HEX "hex" |
| 39 | +#define RUSTG_RNG_FORMAT_ALPHANUMERIC "alphanumeric" |
| 40 | +#define RUSTG_RNG_FORMAT_BASE32 "base32_rfc4648" |
| 41 | +#define RUSTG_RNG_FORMAT_BASE32_PADDED "base32_rfc4648_pad" |
| 42 | +#define RUSTG_RNG_FORMAT_BASE64 "base64" |
5 | 43 |
|
6 | 44 | #define RUSTG_HASH_MD5 "md5" |
7 | 45 | #define RUSTG_HASH_SHA1 "sha1" |
8 | 46 | #define RUSTG_HASH_SHA256 "sha256" |
9 | 47 | #define RUSTG_HASH_SHA512 "sha512" |
10 | 48 | #define RUSTG_HASH_XXH64 "xxh64" |
| 49 | +#define RUSTG_HASH_BASE32 "base32_rfc4648" |
| 50 | +#define RUSTG_HASH_BASE32_PADDED "base32_rfc4648_pad" |
11 | 51 | #define RUSTG_HASH_BASE64 "base64" |
12 | 52 |
|
13 | 53 | /// Encode a given string into base64 |
14 | 54 | #define rustg_encode_base64(str) rustg_hash_string(RUSTG_HASH_BASE64, str) |
15 | 55 | /// Decode a given base64 string |
16 | 56 | #define rustg_decode_base64(str) RUSTG_CALL(RUST_G, "decode_base64")(str) |
17 | 57 |
|
| 58 | +/// Encode a given string into base32 (RFC4648) |
| 59 | +/// If padding set to FALSE, will not output padding characters. |
| 60 | +#define rustg_encode_base32(str, padding) rustg_hash_string(padding ? RUSTG_HASH_BASE32_PADDED : RUSTG_HASH_BASE32, str) |
| 61 | +/// Decode a given base32 (RFC4648) string |
| 62 | +/// If padding set to FALSE, decoding will not support padding characters. |
| 63 | +#define rustg_decode_base32(str, padding) RUSTG_CALL(RUST_G, "decode_base32")(str, "[padding ? 1 : 0]") |
| 64 | + |
18 | 65 | #ifdef RUSTG_OVERRIDE_BUILTINS |
19 | 66 | #define md5(thing) (isfile(thing) ? rustg_hash_file(RUSTG_HASH_MD5, "[thing]") : rustg_hash_string(RUSTG_HASH_MD5, thing)) |
20 | 67 | #endif |
0 commit comments