sophuwu.site > seks
added marshall as module
sophuwu sophie@sophuwu.site
Sun, 15 Oct 2023 15:33:30 +0200
commit

5b6e57ad5940dca22134bb4746d6c86b733a6191

parent

0420dbe756a793071773fbc220348b260fc4b7fa

3 files changed, 70 insertions(+), 51 deletions(-)

jump to
M main.gomain.go

@@ -8,43 +8,9 @@ "crypto/rand"

"crypto/sha256" "fmt" "golang.org/x/crypto/nacl/secretbox" - _ "time" + "seks/sopHex" ) -const hexKey = "SOPHIE+MAL1VN=<3" - -func marshoph(b []byte) string { - var s string - for i := 0; i < len(b); i++ { - s += string(hexKey[(b[i]>>4)&15]) + string(hexKey[b[i]&15]) - } - return s -} - -func parsoph(s string) []byte { - var b []byte - if len(s)%2 != 0 { - panic("hexUnString: odd length string") - } - var err error - index := func(c byte) int { - for j, v := range hexKey { - if v == rune(c) { - return j - } - } - err = fmt.Errorf("parare: invalid character %c", c) - return 0 - } - for i := 0; i < len(s); i += 2 { - b = append(b, byte(index(s[i])<<4|index(s[i+1]))) - if err != nil { - panic(err) - } - } - return b -} - func ran() []byte { var b [32]byte _, err := rand.Reader.Read(b[:])

@@ -62,8 +28,7 @@ // User makes a password for each bucket

passwd := []byte("TestPassword") - //salt := ran() - salt := parsoph("3MN+1SML3OV<IVO1OMNENNL<MIOSMPP1ONOSL+=+NMPPVEOA++A<LPH1S3L+IIHA") + salt := ran() hash := sha256.New() hash.Write(passwd) hash.Write(salt)

@@ -71,20 +36,20 @@

var key [32]byte copy(key[:], hash.Sum(nil)) - //var nonce = [24]byte(ran()[0:24]) + var nonce = [24]byte(ran()[0:24]) + salt = append(salt[:], nonce[:]...) + message := []byte("I like to eat apples and bananas. However, I do not like to eat oranges. Cars can drive!") + encrypted := secretbox.Seal(nil, message, &nonce, &key) - //message := []byte("I like to eat apples and bananas. However, I do not like to eat oranges. Cars can drive!") - //encrypted := secretbox.Seal(nonce[:], message, &nonce, &key) - - //fmt.Println(marshoph(encrypted)) - //fmt.Println(marshoph(salt)) + fmt.Println(sopHex.Marshall(encrypted)) + /* + deNonce := [24]byte(encrypted[0:24]) + decrypted, boolEnlon := secretbox.Open(nil, encrypted[24:], &deNonce, &key) + if boolEnlon != true { + fmt.Println("OOPS") + return + } + fmt.Println(string(decrypted)) - encrypted := parsoph("AHNS=+H+IMNH+OHOAHI+A==3SVVLMVII<O+NH3MSE3=SO+<SNMI1EP1+LONHHE3==N1OESVPSMNHN1=1PNHHSVAPHE+HV31O+OVE3N3IM+OAAA+1=LSO<N+<+IA<S1P+HLM<+=S<EEA<31=O1OSOM1IN3HEV<AILHAS<+S+ONVVLM3=V=+OPLLNSLH<S+=LPSVMM<SPIMNE<EV<MIPOOH+NHO<3A3=IS+ESS3E+SMPEL313MOVL1<N1INSAAE1HS") - deNonce := [24]byte(encrypted[0:24]) - decrypted, boolEnlon := secretbox.Open(nil, encrypted[24:], &deNonce, &key) - if boolEnlon != true { - fmt.Println("OOPS") - return - } - fmt.Println(string(decrypted)) + */ }
A sopHex/go.mod

@@ -0,0 +1,3 @@

+module sopHex + +go 1.21.3
A sopHex/sopHex.go

@@ -0,0 +1,51 @@

+package sopHex + +import "fmt" + +const sopHexSet string = "SOPHIE+MAL1VN=<3" + +const seksHeader string = `-----BEGIN SEKS SECRET----- +` +const seksFooter string = ` +-----END SEKS SECRET----- +` + +func Marshall(b []byte) string { + var s string + for i := 0; i < len(b); i++ { + if i%16 == 0 { + s += "\n" + } + s += string(sopHexSet[(b[i]>>4)&15]) + string(sopHexSet[b[i]&15]) + } + return seksHeader + s + seksFooter +} + +func UnMarshall(s string) ([]byte, error) { + if len(s)%2 != 0 { + return nil, fmt.Errorf("sopHex UnMarshall: invalid length %d", len(s)) + } + var b []byte + var n int + for i, v := range s { + n = index(v) + if n == -1 { + return nil, fmt.Errorf("sopHex 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 sopHexSet { + if v == c { + return j + } + } + return -1 +}