Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,21 @@ crypto.from_json("{\"foo\":\"bar\"}")
}
```

### crypto.hash_file
### crypto.is_json

`crypto.hash_file(file: str, algo: str) -> str`
`crypto.is_json(content: str) -> bool`

The <b>crypto.hash_file</b> method will produce the hash of the given file's contents. Valid algorithms include:
The <b>crypto.is_json</b> tests if JSON is valid.

- MD5
- SHA1
- SHA256
- SHA512
```python
crypto.is_json("{\"foo\":\"bar\"}")
True
```

```python
crypto.is_json("foobar")
False
```

### crypto.to_json

Expand All @@ -264,6 +269,17 @@ crypto.to_json({"foo": "bar"})
"{\"foo\":\"bar\"}"
```

### crypto.hash_file

`crypto.hash_file(file: str, algo: str) -> str`

The <b>crypto.hash_file</b> method will produce the hash of the given file's contents. Valid algorithms include:

- MD5
- SHA1
- SHA256
- SHA512

---

## File
Expand Down
32 changes: 32 additions & 0 deletions implants/lib/eldritch/src/crypto/is_json_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use anyhow::Result;

pub fn is_json(json: String) -> Result<bool> {
match serde_json::from_str::<serde_json::Value>(&json) {
Ok(_) => Ok(true),
Err(_) => Ok(false),
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_from_json_object() -> anyhow::Result<()> {
let res = super::is_json(r#"{"test": "test"}"#.to_string())?;
assert!(res);
Ok(())
}

#[test]
fn test_from_json_list() -> anyhow::Result<()> {
let res = super::is_json(r#"[1, "foo", false, null]"#.to_string())?;
assert!(res);
Ok(())
}

#[test]
fn test_from_json_invalid() -> anyhow::Result<()> {
let res = super::is_json(r#"{"test":"#.to_string())?;
assert!(!res);
Ok(())
}
}
7 changes: 7 additions & 0 deletions implants/lib/eldritch/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod decode_b64_impl;
mod encode_b64_impl;
mod from_json_impl;
mod hash_file_impl;
mod is_json_impl;
mod to_json_impl;

use starlark::environment::MethodsBuilder;
Expand Down Expand Up @@ -52,6 +53,12 @@ fn methods(builder: &mut MethodsBuilder) {
decode_b64_impl::decode_b64(content, encode_type)
}

#[allow(unused_variables)]
fn is_json<'v>(this: &CryptoLibrary, starlark_heap: &'v Heap, content: String) -> anyhow::Result<bool> {
is_json_impl::is_json(content)
}


#[allow(unused_variables)]
fn from_json<'v>(this: &CryptoLibrary, starlark_heap: &'v Heap, content: String) -> anyhow::Result<Value<'v>> {
from_json_impl::from_json(starlark_heap, content)
Expand Down
2 changes: 1 addition & 1 deletion implants/lib/eldritch/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ mod tests {
parameters: HashMap::new(),
file_names: Vec::new(),
},
want_text: format!("{}\n", r#"["aes_decrypt_file", "aes_encrypt_file", "decode_b64", "encode_b64", "from_json", "hash_file", "to_json"]"#),
want_text: format!("{}\n", r#"["aes_decrypt_file", "aes_encrypt_file", "decode_b64", "encode_b64", "from_json", "hash_file", "is_json", "to_json"]"#),
want_error: None,
},
time_bindings: TestCase {
Expand Down
Loading