|
203 | 203 |
|
204 | 204 | #define rustg_hash_string(algorithm, text) RUSTG_CALL(RUST_G, "hash_string")(algorithm, text) |
205 | 205 | #define rustg_hash_file(algorithm, fname) RUSTG_CALL(RUST_G, "hash_file")(algorithm, fname) |
206 | | -#define rustg_hash_generate_totp(seed) RUSTG_CALL(RUST_G, "generate_totp")(seed) |
207 | | -#define rustg_hash_generate_totp_tolerance(seed, tolerance) RUSTG_CALL(RUST_G, "generate_totp_tolerance")(seed, tolerance) |
| 206 | + |
| 207 | +/// Supported algorithms: RUSTG_HASH_SHA1, RUSTG_HASH_SHA256, RUSTG_HASH_SHA512 |
| 208 | +/// Seed must be between 10 bytes to 64 bytes (padded or unpadded) of base32. 20 bytes is recommended. Use a CSPRNG. |
| 209 | +/// Refresh rate is fixed at 30sec and digit count is fixed at 6 |
| 210 | +#define rustg_hash_generate_totp(algorithm, seed) RUSTG_CALL(RUST_G, "generate_totp")(algorithm, seed) |
| 211 | +/// Supported algorithms: RUSTG_HASH_SHA1, RUSTG_HASH_SHA256, RUSTG_HASH_SHA512 |
| 212 | +/// Seed must be between 10 bytes to 64 bytes (padded or unpadded) of base32. 20 bytes is recommended. Use a CSPRNG. |
| 213 | +/// Refresh rate is fixed at 30sec and digit count is fixed at 6 |
| 214 | +/// Tolerance is the number of codes +-30sec from the current one that are allowed. |
| 215 | +#define rustg_hash_generate_totp_tolerance(algorithm, seed, tolerance) RUSTG_CALL(RUST_G, "generate_totp_tolerance")(algorithm, seed, tolerance) |
| 216 | + |
| 217 | +/// Creates a cryptographically-secure pseudorandom number generator using the OS-level PRNG as a seed |
| 218 | +/// n_bytes is the number of bytes provided to the RNG, the length of the string output varies by format |
| 219 | +/// The output string length and characters contained in each format is as follows: |
| 220 | +/// RUSTG_RNG_FORMAT_HEX: n_bytes * 2, [a-z0-9] |
| 221 | +/// RUSTG_RNG_FORMAT_ALPHANUMERIC: n_bytes, [A-Za-z0-9] |
| 222 | +/// RUSTG_RNG_FORMAT_BASE32: ceil(n_bytes / 5 * 8) [A-Z2-7] |
| 223 | +/// RUSTG_RNG_FORMAT_BASE32_PADDED: ceil(n_bytes / 5) * 8 [A-Z2-7=] |
| 224 | +/// RUSTG_RNG_FORMAT_BASE64: 4 * ceil(n_bytes/3), [A-Za-z0-9+/=] |
| 225 | +/// Outputs "ERROR: [reason]" if the format string provided is invalid, or n_bytes is not a positive non-zero integer |
| 226 | +#define rustg_csprng_chacha20(format, n_bytes) RUSTG_CALL(RUST_G, "csprng_chacha20")(format, "[n_bytes]") |
| 227 | + |
| 228 | +/// Creates a seeded pseudorandom number generator using the SHA256 hash output bytes of the seed string |
| 229 | +/// Note that this function is NOT suitable for use in cryptography and is intended for high-quality **predictable** RNG |
| 230 | +/// Use rustg_csprng_chacha20 for a cryptographically-secure PRNG. |
| 231 | +/// n_bytes is the number of bytes provided to the RNG, the length of the string output varies by format |
| 232 | +/// The output string length and characters contained in each format is as follows: |
| 233 | +/// RUSTG_RNG_FORMAT_HEX: n_bytes * 2, [a-z0-9] |
| 234 | +/// RUSTG_RNG_FORMAT_ALPHANUMERIC: n_bytes, [A-Za-z0-9] |
| 235 | +/// RUSTG_RNG_FORMAT_BASE32: ceil(n_bytes / 5 * 8) [A-Z2-7] |
| 236 | +/// RUSTG_RNG_FORMAT_BASE32_PADDED: ceil(n_bytes / 5) * 8 [A-Z2-7=] |
| 237 | +/// RUSTG_RNG_FORMAT_BASE64: 4 * ceil(n_bytes/3), [A-Za-z0-9+/=] |
| 238 | +/// Outputs "ERROR: [reason]" if the format string provided is invalid, or n_bytes is not a positive non-zero integer |
| 239 | +#define rustg_prng_chacha20_seeded(format, n_bytes, seed) RUSTG_CALL(RUST_G, "prng_chacha20_seeded")(format, "[n_bytes]", seed) |
| 240 | + |
| 241 | +#define RUSTG_RNG_FORMAT_HEX "hex" |
| 242 | +#define RUSTG_RNG_FORMAT_ALPHANUMERIC "alphanumeric" |
| 243 | +#define RUSTG_RNG_FORMAT_BASE32 "base32_rfc4648" |
| 244 | +#define RUSTG_RNG_FORMAT_BASE32_PADDED "base32_rfc4648_pad" |
| 245 | +#define RUSTG_RNG_FORMAT_BASE64 "base64" |
208 | 246 |
|
209 | 247 | #define RUSTG_HASH_MD5 "md5" |
210 | 248 | #define RUSTG_HASH_SHA1 "sha1" |
211 | 249 | #define RUSTG_HASH_SHA256 "sha256" |
212 | 250 | #define RUSTG_HASH_SHA512 "sha512" |
213 | 251 | #define RUSTG_HASH_XXH64 "xxh64" |
| 252 | +#define RUSTG_HASH_BASE32 "base32_rfc4648" |
| 253 | +#define RUSTG_HASH_BASE32_PADDED "base32_rfc4648_pad" |
214 | 254 | #define RUSTG_HASH_BASE64 "base64" |
215 | 255 |
|
216 | 256 | /// Encode a given string into base64 |
217 | 257 | #define rustg_encode_base64(str) rustg_hash_string(RUSTG_HASH_BASE64, str) |
218 | | -/// Decode a given base64 string |
| 258 | +/// Decode a given base64 string. This expects padding. |
| 259 | +/// Returns a blank string if the string is not valid base64. |
219 | 260 | #define rustg_decode_base64(str) RUSTG_CALL(RUST_G, "decode_base64")(str) |
220 | 261 |
|
| 262 | +/// Encode a given string into base32 (RFC4648) |
| 263 | +/// If padding set to FALSE, will not output padding characters. |
| 264 | +#define rustg_encode_base32(str, padding) rustg_hash_string(padding ? RUSTG_HASH_BASE32_PADDED : RUSTG_HASH_BASE32, str) |
| 265 | +/// Decode a given base32 (RFC4648) string |
| 266 | +/// If padding set to FALSE, decoding will not support padding characters. |
| 267 | +/// Returns a blank string if the string is not valid base32. |
| 268 | +#define rustg_decode_base32(str, padding) RUSTG_CALL(RUST_G, "decode_base32")(str, "[padding ? 1 : 0]") |
| 269 | + |
221 | 270 | #ifdef RUSTG_OVERRIDE_BUILTINS |
222 | 271 | #define md5(thing) (isfile(thing) ? rustg_hash_file(RUSTG_HASH_MD5, "[thing]") : rustg_hash_string(RUSTG_HASH_MD5, thing)) |
223 | 272 | #endif |
|
0 commit comments