using normal base64
sophuwu sophie@skisiel.com
Tue, 23 Jul 2024 21:59:00 +0200
4 files changed,
19 insertions(+),
73 deletions(-)
M
go.mod
→
go.mod
@@ -1,10 +1,7 @@
-module git.sophuwu.site/seks +module sophuwu.site/seks go 1.21.3 -require ( - golang.org/x/crypto v0.14.0 - golang.org/x/term v0.13.0 -) +require golang.org/x/crypto v0.14.0 -require golang.org/x/sys v0.13.0 // indirect+require golang.org/x/sys v0.13.0 // indirect
M
go.sum
→
go.sum
@@ -2,5 +2,3 @@ golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= -golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
M
seks.go
→
seks.go
@@ -3,7 +3,7 @@
import ( "crypto/rand" "crypto/sha256" - "fmt" + "encoding/base64" "golang.org/x/crypto/nacl/secretbox" "strings" )@@ -25,34 +25,16 @@ copy(key[:], hash.Sum(nil))
return key } -const seksArmour = `0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_` -const seksHeader = `-----BEGIN SOME ENCRYPTION KEY STUFF-----` -const seksFooter = `------END SOME ENCRYPTION KEY STUFF------` +const seksHeader = "-----BEGIN SOME ENCRYPTION KEY STUFF-----\n\n" +const seksFooter = "\n------END SOME ENCRYPTION KEY STUFF------" 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) - } - 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 - } - } - // add newlines - for i := 0; i < len(s); i += len(seksHeader) { + s = base64.StdEncoding.EncodeToString(b) + for i := 64; i < len(s); i += 64 { s = s[:i] + "\n" + s[i:] } - return seksHeader + s + "\n" + seksFooter + "\n" + return seksHeader + s + seksFooter } func Encrypt(data string, password string) string {@@ -87,45 +69,10 @@ return decrypted
} func unArmour(s string) ([]byte, error) { - s = strings.ReplaceAll(s, "\t", "") + start := strings.Index(s, seksHeader) + len(seksHeader) + end := strings.Index(s, seksFooter) + s = s[start:end] 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 - } - } - if len(b) > 0 { // remove padding - for i := 0; i < 3; i++ { - if b[len(b)-1] == 0 { - b = b[:len(b)-1] - } - } - } - return b, nil -} - -func index(c rune) (j int) { - for j, v := range seksArmour { - if v == c { - return j - } - } - return -1 + return base64.StdEncoding.DecodeString(s) }
M
test/test.go
→
test/test.go
@@ -2,8 +2,8 @@ package main
import ( "fmt" - "git.sophuwu.site/seks" "os" + "sophuwu.site/seks" ) func main() {@@ -19,7 +19,11 @@ 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]) + s, err := seks.Decrypt(string(b), os.Args[2]) + if err != nil { + fmt.Println(err) + os.Exit(1) + } fmt.Println(s) } }