aboutsummaryrefslogtreecommitdiff
path: root/internal/crud/raw_Encrypt.go
diff options
context:
space:
mode:
authorishaan <contact@ishaan.cc>2026-07-12 08:07:47 +0530
committerishaan <contact@ishaan.cc>2026-07-12 08:07:47 +0530
commite9f777d8f7b8b77993f0b88032c33bbef06959c0 (patch)
tree7ad8945fcb4340b8db1ecad6f75fe2a19986667c /internal/crud/raw_Encrypt.go
parent65017dfaff1376a16f4d11e76a8aa2d625e05aec (diff)
implement headers
Diffstat (limited to 'internal/crud/raw_Encrypt.go')
-rw-r--r--internal/crud/raw_Encrypt.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/internal/crud/raw_Encrypt.go b/internal/crud/raw_Encrypt.go
new file mode 100644
index 0000000..ec2b4f6
--- /dev/null
+++ b/internal/crud/raw_Encrypt.go
@@ -0,0 +1,60 @@
+package crud
+
+import (
+ "fmt"
+ "os"
+
+ "gitlab.com/whiteboardguy/eno/internal/utils"
+ "golang.org/x/crypto/argon2"
+ "golang.org/x/crypto/chacha20poly1305"
+)
+
+func RawEncryptFile(inputFile, outputFile string) {
+ if _, err := os.ReadFile(outputFile); err == nil {
+ var overwriteConfirm string
+ fmt.Printf("file '%s' already exists. Overwrite? [y/N] ", outputFile)
+ fmt.Scanln(&overwriteConfirm)
+
+ if overwriteConfirm != "y" && overwriteConfirm != "Y" {
+ fmt.Fprintln(os.Stderr, "write aborted")
+ os.Exit(1)
+ }
+ }
+
+ fileBytes, err := os.ReadFile(inputFile)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err.Error())
+ os.Exit(1)
+ }
+
+ fmt.Printf("Enter a super secure passphrase: ")
+ password, err := utils.TakeSecureUserInput()
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err.Error())
+ os.Exit(1)
+ }
+
+ headers := NewFileHeaders()
+ fmt.Println(headers)
+
+ key := argon2.IDKey(password, headers.Salt[:], 1, 2*1024*1024, 2, 32)
+ aead, err := chacha20poly1305.New(key)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err.Error())
+ os.Exit(1)
+ }
+
+ encrypted := aead.Seal(nil, headers.Nonce[:], fileBytes, nil)
+
+ headerBytes := FileHeadersBytes(headers)
+
+ target := make([]byte, 0, len(headerBytes)+len(encrypted)+20)
+ target = fmt.Appendf(target, "%d:", len(headerBytes))
+ target = append(target, headerBytes...)
+ target = append(target, encrypted...)
+
+ if err := os.WriteFile(outputFile, target, 0644); err != nil {
+ fmt.Fprintln(os.Stderr, err.Error())
+ os.Exit(1)
+ }
+}