Skip to content

Commit

Permalink
add leet encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
ipsax committed Nov 25, 2024
1 parent bfb4a19 commit 12af7d4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
14 changes: 11 additions & 3 deletions cmd/leet.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package cmd

import (
"fmt"

"github.com/ipsax/kypher/internal"
"github.com/spf13/cobra"
)

var encodeArg string

func init() {
leetCmd.PersistentFlags().StringVarP(&encodeArg, "encode", "e", "", "Leet message value to be encoded")
rootCmd.AddCommand(leetCmd)
}

var leetCmd = &cobra.Command{
Use: "leet",
Short: "use the 1337 cipher",
Use: "leet [OPTIONS]",
Short: "use the leet cipher",
Long: `This is kypher's leet cipher`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
leet := internal.Leet{}
leet.Encode("todo, take cli args for encode or decode, maybe add autodetect for autodecode eventually?")
if encodeArg != "" {
fmt.Println(leet.Encode(encodeArg))
}
},
}
24 changes: 19 additions & 5 deletions internal/leet.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package internal

import (
"fmt"
"math/rand"
)

type Leet struct{}

// todo make type for leetchars = https://en.wikipedia.org/wiki/Leet#Orthography
func leetIterator() {
func leetMap() map[string][]string {
var a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z []string
a = append(a, "a", "4", "/", `\`, `@`, "/-", "^", "(L", "Д")
b = append(b, "b", "I3", "8", "13", "|3", "ß", "!3", "(3", "/3", ")3", "|-]", "j3")
Expand Down Expand Up @@ -64,12 +64,26 @@ func leetIterator() {
leetChars["y"] = y
leetChars["z"] = z

fmt.Println(leetChars)
return leetChars
}

func randRange(min, max int) int {
return rand.Intn(max-min) + min
}

func (Leet) Encode(str string) string {
leetIterator()
return "todo return str->leet cipher"
encoded := ""
leetChars := leetMap()

for _, c := range str {
// todo ignore chars that arent a-z
chars, exists := leetChars[string(c)]
if exists {
selectedLeet := chars[randRange(0, len(chars))]
encoded += selectedLeet
}
}
return encoded
}

func (Leet) Decode(str string) string {
Expand Down

0 comments on commit 12af7d4

Please sign in to comment.