Skip to content

Commit

Permalink
Add salt builtin
Browse files Browse the repository at this point in the history
Note: Not compatible with function of same name in ToastStunt
  • Loading branch information
rdaum committed Feb 15, 2025
1 parent c28bcba commit badbe75
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
7 changes: 7 additions & 0 deletions crates/compiler/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,13 @@ fn mk_builtin_table() -> Vec<Builtin> {
types: vec![Any],
implemented: true,
},
Builtin {
name: Symbol::mk("salt"),
min_args: Q(0),
max_args: Q(0),
types: vec![],
implemented: true,
},
]
}

Expand Down
18 changes: 17 additions & 1 deletion crates/kernel/src/builtins/bf_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use moor_values::Error::{E_ARGS, E_INVARG, E_TYPE};
use moor_values::{v_int, v_str, v_string};
use moor_values::{Sequence, Variant};
use rand::distributions::Alphanumeric;
use rand::Rng;
use rand::{thread_rng, Rng};
use tracing::warn;

use crate::bf_declare;
Expand Down Expand Up @@ -168,6 +168,21 @@ fn bf_strcmp(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
}
bf_declare!(strcmp, bf_strcmp);

/// Generate a random cryptographically secure salt string, for use with crypt & argon2
/// Note: This is not (for now) compatible with the `salt` function in ToastStunt, which takes
/// two arguments.
fn bf_salt(bf_args: &mut BfCallState<'_>) -> Result<BfRet, BfErr> {
if !bf_args.args.is_empty() {
return Err(BfErr::Code(E_ARGS));
}

let mut rng_core = thread_rng();
let salt = SaltString::generate(&mut rng_core);
let salt = v_str(salt.as_str());
Ok(Ret(salt))
}
bf_declare!(salt, bf_salt);

/*
str crypt (str text [, str salt])
Expand Down Expand Up @@ -336,6 +351,7 @@ pub(crate) fn register_bf_strings(builtins: &mut [Box<dyn BuiltinFunction>]) {
builtins[offset_for_builtin("argon2_verify")] = Box::new(BfArgon2Verify {});
builtins[offset_for_builtin("string_hash")] = Box::new(BfStringHash {});
builtins[offset_for_builtin("binary_hash")] = Box::new(BfBinaryHash {});
builtins[offset_for_builtin("salt")] = Box::new(BfSalt {});
}

#[cfg(test)]
Expand Down
45 changes: 23 additions & 22 deletions doc/builtin_functions_status.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ included in the notes column.
### Lists

| Name | Complete | Notes |
| ------------ | -------- | ------------------------- |
|--------------|----------|---------------------------|
| `length` | &check; | |
| `setadd` | &check; | |
| `setremove` | &check; | |
Expand All @@ -26,22 +26,23 @@ included in the notes column.

### Strings

| Name | Complete | Notes |
| --------------- | -------- | ------------------------------------------------------------------------------ |
| `tostr` | &check; | |
| `toliteral` | &check; | |
| `crypt` | &check; | Pretty damned insecure, only here to support existing core password functions. |
| `index` | &check; | |
| `rindex` | &check; | |
| `strcmp` | &check; | |
| `strsub` | &check; | |
| `argon2` | &check; | Same signature as function in ToastSunt |
| `arong2_verify` | &check; | Same signature as function in ToastSunt |
| Name | Complete | Notes |
|-----------------|----------|------------------------------------------------------------------------------------------------------|
| `tostr` | &check; | |
| `toliteral` | &check; | |
| `crypt` | &check; | Pretty damned insecure, only here to support existing core password functions. |
| `index` | &check; | |
| `rindex` | &check; | |
| `strcmp` | &check; | |
| `strsub` | &check; | |
| `argon2` | &check; | Same signature as function in ToastSunt |
| `arong2_verify` | &check; | Same signature as function in ToastSunt |
| `salt` | &check; | Generate a random crypto-secure salt for password. Not compatible with toast's function of same name |

### Numbers

| Name | Complete | Notes |
| ---------- | -------- | ----- |
|------------|----------|-------|
| `toint` | &check; | |
| `tonum` | &check; | |
| `tofloat` | &check; | |
Expand Down Expand Up @@ -72,7 +73,7 @@ included in the notes column.
### Objects

| Name | Complete | Notes |
| ----------------- | -------- | ---------------------------------- |
|-------------------|----------|------------------------------------|
| `toobj` | &check; | |
| `typeof` | &check; | |
| `create` | &check; | Quota support not implemented yet. |
Expand All @@ -90,7 +91,7 @@ included in the notes column.
### Properties

| Name | Complete | Notes |
| ------------------- | -------- | ----- |
|---------------------|----------|-------|
| `properties` | &check; | |
| `property_info` | &check; | |
| `set_property_info` | &check; | |
Expand All @@ -102,7 +103,7 @@ included in the notes column.
### Verbs

| Name | Complete | Notes |
| --------------- | -------- | ------------------------------------- |
|-----------------|----------|---------------------------------------|
| `verbs` | &check; | |
| `verb_info` | &check; | |
| `set_verb_info` | &check; | |
Expand All @@ -118,7 +119,7 @@ included in the notes column.
### Values / encoding

| Name | Complete | Notes |
| --------------- | -------- | ---------------------------------------------------------------------------------- |
|-----------------|----------|------------------------------------------------------------------------------------|
| `value_bytes` | &check; | |
| `value_hash` | | |
| `string_hash` | &check; | |
Expand All @@ -130,7 +131,7 @@ included in the notes column.
### Server

| Name | Complete | Notes |
| --------------------- | -------- | ------------------------------------------------------------------------ |
|-----------------------|----------|--------------------------------------------------------------------------|
| `server_version` | &check; | Crate version + short commit hash, for now |
| `renumber` | | |
| `reset_max_object` | | |
Expand All @@ -152,7 +153,7 @@ included in the notes column.
### Tasks

| Name | Complete | Notes |
| -------------- | -------- | ----- |
|----------------|----------|-------|
| `task_id` | &check; | |
| `queued_tasks` | &check; | |
| `kill_task` | &check; | |
Expand All @@ -164,7 +165,7 @@ included in the notes column.
### Execution

| Name | Complete | Notes |
| ---------------- | -------- | ------------ |
|------------------|----------|--------------|
| `call_function` | &check; | |
| `raise` | &check; | |
| `suspend` | &check; | |
Expand All @@ -179,7 +180,7 @@ included in the notes column.
### Network connections

| Name | Complete | Notes |
| ------------------------- | -------- | ---------------------------------------------------------------------------------------------------- |
|---------------------------|----------|------------------------------------------------------------------------------------------------------|
| `set_connection_option` | | |
| `connection_option` | | |
| `connection_options` | | |
Expand All @@ -197,6 +198,6 @@ Functions not part of the original LambdaMOO, but added in moor
### XML / HTML content management

| Name | Description | Notes |
| ----------- | ---------------------------------------------------------------- | ----------------------------------------------------- |
|-------------|------------------------------------------------------------------|-------------------------------------------------------|
| `xml_parse` | Parse a string c ntaining XML into a tree of flyweight objects | Available only if the flyweights feature is turned on |
| `to_xml` | Convert a tree of flyweight objects into a string containing XML | Available only if the flyweights feature is turned on |

0 comments on commit badbe75

Please sign in to comment.