Skip to content

Commit

Permalink
Adds Base64 decoding stuff (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
AffectedArc07 authored Dec 17, 2023
1 parent b964c87 commit 9cfa350
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dmsrc/hash.dm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
#define RUSTG_HASH_XXH64 "xxh64"
#define RUSTG_HASH_BASE64 "base64"

/// Encode a given string into base64
#define rustg_encode_base64(str) rustg_hash_string(RUSTG_HASH_BASE64, str)
/// Decode a given base64 string
#define rustg_decode_base64(str) RUSTG_CALL(RUST_G, "decode_base64")(str)

#ifdef RUSTG_OVERRIDE_BUILTINS
#define md5(thing) (isfile(thing) ? rustg_hash_file(RUSTG_HASH_MD5, "[thing]") : rustg_hash_string(RUSTG_HASH_MD5, thing))
#endif
4 changes: 4 additions & 0 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ byond_fn!(fn hash_string(algorithm, string) {
string_hash(algorithm, string).ok()
});

byond_fn!(fn decode_base64(string) {
Some(base64::prelude::BASE64_STANDARD.decode(string).unwrap())
});

byond_fn!(fn hash_file(algorithm, string) {
file_hash(algorithm, string).ok()
});
Expand Down
9 changes: 9 additions & 0 deletions tests/dm/hash.dme
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ var/list/reference = list()

for (var/entry in reference)
check_hash_base64(entry)

// Test B64 decode stuff
var/input_str = "dGhpcyBpcyBzb21lIHRleHQ="
var/expected_output = "this is some text"
var/actual = rustg_decode_base64(input_str)

if (!cmptextEx(expected_output, actual))
CRASH("Base64 decode failed | S: [input_str] | E: [expected_output] | A: [actual]")

0 comments on commit 9cfa350

Please sign in to comment.