uwuw woking again yayaya
sophuwu sophie@skisiel.com
Tue, 04 Jun 2024 13:12:50 +0200
3 files changed,
123 insertions(+),
116 deletions(-)
D
encoding/encoding.go
@@ -1,60 +0,0 @@
-package git.sophuwu.site/seks/encoding - -import ( - "fmt" - "strings" -) - -// var SEKSSet string = `SOPHIE+MAL1VN=<3` // short for test purposes -var SEKSSet string = `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$£` - -const seksHeader string = "-----BEGIN SOME ENCRYPTION KEY STUFF-----" -const seksFooter string = "-----END SOME ENCRYPTION KEY STUFF-----" - -func Armour(b []byte) string { - var s string - for i := 0; i < len(b); i++ { - if i%69 == 0 { - s += "\n" - } - s += string(SEKSSet[(b[i]>>4)&15]) + string(SEKSSet[b[i]&15]) - } - return seksHeader + s + "\n" + seksFooter + "\n" -} - -func UnArmour(s string) ([]byte, error) { - begin := strings.Index(s, seksHeader) - end := strings.Index(s, seksFooter) - if begin < 0 || end < 0 { - return nil, fmt.Errorf("SEKS UnMarshall: invalid seks secret") - } - s = s[begin+len(seksHeader) : end] - s = strings.ReplaceAll(s, "\t", "") - s = strings.ReplaceAll(s, "\n", "") - s = strings.TrimPrefix(s, seksHeader) - s = strings.TrimSuffix(s, seksFooter) - s = strings.ReplaceAll(s, " ", "") - var b []byte - var n int - for i, v := range s { - n = index(v) - if n == -1 { - return nil, fmt.Errorf("SEKS UnMarshall: invalid character %q at index %d", v, i) - } - if i%2 == 0 { - b = append(b, byte(n<<4)) - } else { - b[len(b)-1] |= byte(n) - } - } - return b, nil -} - -func index(c rune) (j int) { - for j, v := range SEKSSet { - if v == c { - return j - } - } - return -1 -}
M
seks.go
→
seks.go
@@ -1,9 +1,11 @@
-package git.sophuwu.site/seks +package seks import ( "crypto/rand" "crypto/sha256" + "fmt" "golang.org/x/crypto/nacl/secretbox" + "strings" ) func ran() []byte {@@ -14,16 +16,6 @@ panic(err)
} return b[:] } - -func decrypt(encrypted []byte, key [32]byte) string { - deNonce := [24]byte(encrypted[32 : 32+24]) - decrypted, boolEnlon := secretbox.Open(nil, encrypted[32+24:], &deNonce, &key) - if boolEnlon != true { - return "Error decrypting" - } - return string(decrypted) -} - func hashPasswd(salt []byte, passwd []byte) [32]byte { hash := sha256.New() hash.Write(passwd)@@ -32,58 +24,108 @@ var key [32]byte
copy(key[:], hash.Sum(nil)) return key } -/* -func main() { - // NaCl crypto_box symmetric encryption - // Make a bbolt database - // Add buckets for categories of secrets - // User makes a password for each bucket + +const seksArmour = `0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_` +const seksHeader = `-----BEGIN SOME ENCRYPTION KEY STUFF-----` +const seksFooter = `------END SOME ENCRYPTION KEY STUFF------` - if len(os.Args) < 2 { - fmt.Println("Usage: seks -e|-d") - return +func armour(b []byte) string { + var s string + var n uint = 0 + var m uint = 0 + for i := 0; i < 3-len(b)%3; i++ { + b = append(b, 0) } - e := false - if os.Args[1] == "-e" { - e = true - fmt.Println("Encrypting. ") - } else if os.Args[1] == "-d" { - fmt.Println("Decrypting. ") - } else { - fmt.Println("Usage: seks -e|-d") - return + for _, c := range b { + n |= uint(c) << uint(m*8) + m++ + if m == 3 { + for j := 0; j < 4; j++ { + s += string(seksArmour[n&63]) + n >>= 6 + } + m = 0 + n = 0 + } } - fmt.Print("Enter password Your Password: ") - password, err := term.ReadPassword(int(syscall.Stdin)) - if err != nil { - fmt.Println(err) - return + // add newlines + for i := 0; i < len(s); i += len(seksHeader) { + s = s[:i] + "\n" + s[i:] } - var buff bytes.Buffer - fmt.Printf("\nEnter input data ending with EOF (Ctrl-D):\n") - _, err = buff.ReadFrom(os.Stdin) + return seksHeader + s + "\n" + seksFooter + "\n" +} + +func Encrypt(data string, password string) string { + return armour(encryptBytes([]byte(data), password)) +} +func encryptBytes(data []byte, password string) []byte { + salt := ran() + key := hashPasswd(salt, []byte(password)) + var nonce = [24]byte(ran()[0:24]) + salt = append(salt[:], nonce[:]...) + return secretbox.Seal(salt, data, &nonce, &key) +} + +func Decrypt(data string, password string) (string, error) { + b, err := unArmour(data) if err != nil { - fmt.Println(err) - return + return "", err + } + return string(decryptBytes(b, password)), nil +} + +func decryptBytes(encrypted []byte, pass string) []byte { + salt := encrypted[:32] + nonce := [24]byte(encrypted[32 : 32+24]) + key := hashPasswd(salt[:], []byte(pass)) + decrypted := make([]byte, len(encrypted)-32-24) + decrypted, boolEnlon := secretbox.Open(nil, encrypted[32+24:], &nonce, &key) + if boolEnlon != true { + return nil + } + return decrypted +} + +func unArmour(s string) ([]byte, error) { + s = strings.ReplaceAll(s, "\t", "") + s = strings.ReplaceAll(s, "\n", "") + s = strings.TrimPrefix(s, seksHeader) + s = strings.TrimSuffix(s, seksFooter) + s = strings.ReplaceAll(s, " ", "") + var b []byte + var n uint = 0 + var m uint = 0 + for _, c := range s { + i := index(c) + if i < 0 { + return nil, fmt.Errorf("SEKS UnMarshall: invalid character") + } + n |= uint(i) << uint(m*6) + m++ + if m == 4 { + for j := 0; j < 3; j++ { + b = append(b, byte(n&255)) + n >>= 8 + } + m = 0 + n = 0 + } } - var result string - if e { - salt := ran() - key := hashPasswd(salt, password) - var nonce = [24]byte(ran()[0:24]) - salt = append(salt[:], nonce[:]...) - result = sopHex.Marshall(secretbox.Seal(salt, buff.Bytes(), &nonce, &key)) - } else { - var crypt []byte - crypt, err = sopHex.UnMarshall(buff.String()) - if err != nil { - fmt.Println(err) - return + if len(b) > 0 { // remove padding + for i := 0; i < 3; i++ { + if b[len(b)-1] == 0 { + b = b[:len(b)-1] + } } - result = decrypt(crypt, hashPasswd(crypt[0:32], password)) } - fmt.Printf("-----Result-----\n") - fmt.Println(result) + return b, nil +} +func index(c rune) (j int) { + for j, v := range seksArmour { + if v == c { + return j + } + } + return -1 } - */
A
test/test.go
@@ -0,0 +1,25 @@
+package main + +import ( + "fmt" + "git.sophuwu.site/seks" + "os" +) + +func main() { + if len(os.Args) < 4 { + os.Exit(1) + } + b, e := os.ReadFile(os.Args[3]) + if e != nil { + fmt.Println(e) + os.Exit(1) + } + if os.Args[1] == "e" { + fmt.Println(seks.Encrypt(string(b), os.Args[2])) + } + if os.Args[1] == "d" { + s, _ := seks.Decrypt(string(b), os.Args[2]) + fmt.Println(s) + } +}