Created
September 2, 2023 08:55
-
-
Save yunginnanet/852b27b4d7b502b0b77633726d02b1e6 to your computer and use it in GitHub Desktop.
some toy i forgot i wrote
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"git.tcp.direct/kayos/common/entropy" | |
"git.tcp.direct/kayos/common/squish" | |
"github.com/manifoldco/promptui" | |
"os" | |
"strconv" | |
"strings" | |
"time" | |
) | |
const banner = "H4sIAAAAAAACA+OSjrY0tzYxyH00rQPCNAcyW6SjjcGiCtLRBrkgrKAAFwIBZGFS9QOVmUB1QBA2GQjTBGIWkl1Q+Qa4ZhgbRZlBLheSu1CtwHR4C8QYFOfD5bD6FKeJGM7D4WtygwVNK2n+RDfUHLuhBM3DcFYLnqig1HfGUNc0IBzWAOZhswJJAVgNTscTaZ4JLvOQZbBqNTZBWINqCi6HAX3LBQAglQr0jgMAAA==" | |
type Mode uint8 | |
var dataLen = 32 | |
const ( | |
ModeStringGen Mode = iota | |
ModeLength | |
ModeToBytes | |
ModeXOR | |
) | |
func validate(input string) error { | |
if len(strings.TrimSpace(input)) < 1 { | |
return errors.New("no input detected") | |
} | |
return nil | |
} | |
func isNumber(input string) error { | |
_, err := strconv.Atoi(input) | |
return err | |
} | |
func chooseMode() Mode { | |
modePrompt := promptui.Select{ | |
Label: "Choose operation mode", | |
Items: []string{ | |
"Random string of specified length", | |
"Random byte slices of specified length", | |
"Print byte slice form of given string", | |
"XOR random data using the given string as the key", | |
}, | |
CursorPos: 0, | |
IsVimMode: false, | |
} | |
choice, _, _ := modePrompt.Run() | |
return Mode(choice) | |
} | |
func XOR(input []byte, key []byte) []byte { | |
var output = make([]byte, len(input)) | |
head := 0 | |
for i, b := range input { | |
if head == len(key) { | |
head = 0 | |
} | |
output[i] = b ^ key[head] | |
head++ | |
} | |
if len(output) != len(input) { | |
panic("!?") | |
} | |
return output | |
} | |
func stringPrompt(prompt string) string { | |
stringPrompt := promptui.Prompt{ | |
Label: prompt, | |
Default: "fuckhole johnson", | |
Validate: validate, | |
} | |
ret, err := stringPrompt.Run() | |
if err == nil { | |
return ret | |
} | |
println(promptui.IconBad + err.Error()) | |
return "" | |
} | |
func numPrompt(prompt string) int { | |
numPrompt := promptui.Prompt{ | |
Label: prompt, | |
Default: "55555", | |
Validate: func(input string) error { | |
stage1 := validate(input) | |
if stage1 != nil { | |
return stage1 | |
} | |
return isNumber(input) | |
}, | |
} | |
ret, err := numPrompt.Run() | |
if err == nil { | |
retNum, _ := strconv.Atoi(ret) | |
return retNum | |
} | |
println(promptui.IconBad + err.Error()) | |
return -1 | |
} | |
func args() { | |
switch len(os.Args) { | |
case 2: | |
num, err := strconv.Atoi(os.Args[1]) | |
switch { | |
case err == nil: | |
println("Generating random []byte(string) of length " + os.Args[1]) | |
dataLen = num | |
gen() | |
return | |
default: | |
fmt.Fprint(os.Stdout, fmt.Sprint([]byte(os.Args[1]))+"\n") | |
os.Exit(0) | |
} | |
default: | |
mode := chooseMode() | |
if mode == ModeStringGen || mode == ModeLength { | |
dataLen = numPrompt("input length of data to generate") | |
if dataLen < 0 { | |
os.Exit(1) | |
} | |
} | |
switch mode { | |
case ModeStringGen: | |
fmt.Fprint(os.Stdout, entropy.RandStr(dataLen)+"\n") | |
return | |
case ModeLength: | |
gen() | |
return | |
case ModeToBytes: | |
input := stringPrompt("input string") | |
if input == "" { | |
os.Exit(1) | |
} | |
fmt.Fprint(os.Stdout, fmt.Sprint([]byte(input))+"\n") | |
return | |
case ModeXOR: | |
inputTarget := stringPrompt("string to apply XOR bitwise operation to") | |
if inputTarget == "" { | |
println("WARN: No input, using random string of len(32)...") | |
inputTarget = entropy.RandStr(32) | |
} | |
inputKey := stringPrompt("XOR key") | |
if inputKey == "" { | |
os.Exit(1) | |
} | |
res := XOR([]byte(inputTarget), []byte(inputKey)) | |
fmt.Println("Key bytes: ") | |
fmt.Println([]byte(inputKey)) | |
fmt.Println("") | |
fmt.Fprint(os.Stdout, squish.B64e(res)) | |
return | |
} | |
} | |
} | |
func gen() { | |
for { | |
data := []byte(entropy.RandStr(dataLen)) | |
if len(data) != dataLen { | |
panic(data) | |
} | |
fmt.Fprintln(os.Stdout, data) | |
time.Sleep(750 * time.Millisecond) | |
} | |
} | |
func main() { | |
if len(os.Args) != 2 { | |
bnr, _ := squish.UnpackStr(banner) | |
println(bnr) | |
} | |
args() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment