This commit is contained in:
2024-12-25 12:38:51 +01:00
parent 4848703a8b
commit f77c7ba874
50 changed files with 3008 additions and 32 deletions

View File

@@ -0,0 +1,21 @@
module secrets
import rand
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.ui
import freeflowuniverse.herolib.crypt.aes_symmetric
import crypto.md5
import regex
import os
import encoding.base64
// will use our secret as configured for the hero to encrypt
pub fn (mut b SecretBox) encrypt(txt string) !string {
d := aes_symmetric.encrypt_str(txt, b.secret)
return base64.encode_str(d)
}
pub fn (mut b SecretBox) decrypt(txt string) !string {
txt2 := base64.decode_str(txt)
return aes_symmetric.decrypt_str(txt2, b.secret)
}

View File

@@ -0,0 +1,33 @@
module secrets
import freeflowuniverse.herolib.ui.console
pub struct SecretBox {
pub mut:
secret string
items map[string]string
}
@[params]
pub struct SecretBoxArgs {
pub mut:
// reset bool
// interactive bool = true
secret string @[required]
}
pub fn get(args SecretBoxArgs) !SecretBox {
// if args.reset {
// reset()!
// }
// if args.secret.len == 0 {
// mut myui := ui.new()!
// console.clear()
// secret_ := myui.ask_question(question: 'Please enter your hero secret string (box)')!
// secret = md5.hexhash(secret_)
// r.set(key, secret)!
// }
return SecretBox{
secret: args.secret
}
}

View File

@@ -0,0 +1,53 @@
# Secret Box
Some tools to work with encryption/decryption (symmetric)
```go
import freeflowuniverse.herolib.crypt.secrets
mut box:=secrets.get(secret:"mysecret")!
r:= box.encrypt("aaa")!
println(r)
assert "aaa"==box.decrypt(r)!
hex_secret:=secrets.hex_secret()!
openssl_hex_secret:=secrets.openssl_hex_secret()!
openssl_base64_secret:=secrets.openssl_base64_secret()!
```
<!--
## replace some text
some utils to manage secret keys and easily change them in text, ideal for config files.
```go
#!/usr/bin/env -S v -n -cg -w -enable-globals run
import freeflowuniverse.herolib.crypt.secrets
mut box:=secrets.get()!
box.delete("myapp.something")! //make sure we remove all previous keys
//will generate a key (hex of 24 chars) if it doesn't exist yet .
mysecret:=box.secret(key:"myapp.something.a",reset:false)!
println(mysecret)
mut test_string := "This is a test string with {ss} and {MYAPP.SOMETHING.A} and {ABC123}."
test_string1:=box.replace(txt:test_string)!
println(test_string1) -->
test_string2:=box.replace(txt:test_string,defaults:{"MYAPP.SOMETHING.A":secrets.DefaultSecretArgs{secret:"AAA"}})!
println(test_string2)
```

View File

@@ -0,0 +1,76 @@
module secrets
import rand
// import freeflowuniverse.herolib.ui.console
// import freeflowuniverse.herolib.ui
// import freeflowuniverse.herolib.crypt.aes_symmetric
// import crypto.md5
import crypto.sha256
import os
import encoding.base64
@[params]
pub struct SecretArgs {
pub mut:
key string @[required]
default string // if it doesn't exist yet, will create it with this value
overwrite string // will overwrite the secret with this value even if it exists
cat SecretType
// reset bool
}
pub enum SecretType {
normal
openssl_hex
openssl_base64
}
@[params]
pub struct StringArgs {
pub:
input string
}
pub fn hex_secret(args StringArgs) !string {
if args.input == '' {
// If no input string is provided, generate a random hex string
return rand.hex(24)
} else {
// If an input string is provided, use it to generate a consistent hex string
hash := sha256.sum256(args.input.bytes()).hex()
return hash[..48] // Return the first 48 characters (24 bytes) of the hash
}
}
pub fn openssl_hex_secret(args StringArgs) !string {
if args.input == '' {
// If no input string is provided, use the original openssl command
cmd := 'openssl rand -hex 32'
result := os.execute(cmd)
if result.exit_code > 0 {
return error('Command failed with exit code: ${result.exit_code} and error: ${result.output}')
}
return result.output.trim_space()
} else {
// If an input string is provided, use it to generate a consistent hash
hash := sha256.sum256(args.input.bytes()).hex()
return hash[..64] // Return the first 64 characters (32 bytes) of the hash
}
}
pub fn openssl_base64_secret(args StringArgs) !string {
if args.input == '' {
// If no input string is provided, use the original openssl command
cmd := 'openssl rand -base64 32'
result := os.execute(cmd)
if result.exit_code > 0 {
return error('Command failed with exit code: ${result.exit_code} and error: ${result.output}')
}
return result.output.trim_space()
} else {
// If an input string is provided, use it to generate a consistent base64 string
hash := sha256.sum256(args.input.bytes())
base64_str := base64.encode(hash)
return base64_str[..44] // Return the first 44 characters (32 bytes in base64)
}
}

View File

@@ -0,0 +1,28 @@
module secrets
import freeflowuniverse.herolib.ui.console
fn test_check() {
mut box := get(secret: 'mysecret')!
r := box.encrypt('aaa')!
console.print_debug(r)
assert 'aaa' == box.decrypt(r)!
hex_secret1 := hex_secret()!
console.print_debug(hex_secret1)
console.print_debug(hex_secret1.len)
assert hex_secret1.len == 24
openssl_hex_secret1 := openssl_hex_secret()!
console.print_debug(openssl_hex_secret1)
console.print_debug(openssl_hex_secret1.len)
assert openssl_hex_secret1.len == 64
openssl_base64_secret1 := openssl_base64_secret()!
console.print_debug(openssl_base64_secret1)
console.print_debug(openssl_base64_secret1.len)
assert openssl_base64_secret1.len == 44
}