aboutsummaryrefslogtreecommitdiff
path: root/internal/crud/handler_new.go
blob: df0aa6eab47a9e430ba6651079f545374a95d701 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package crud

import (
	"fmt"
	"os"

	"gitlab.com/whiteboardguy/eno/internal/crypt"
	"gitlab.com/whiteboardguy/eno/internal/utils"
)

func NewNote(noteName, output string) error {
	fmt.Printf("Enter the passphrase: ")
	password, err := utils.TakeSecureUserInput()
	if err != nil {
		return fmt.Errorf("failed to read passphrase: %v", err)
	}
	utils.TryMlock(password)
	defer clear(password)

	fmt.Printf("Confirm the passphrase: ")
	passwordConfirm, err := utils.TakeSecureUserInput()
	if err != nil {
		return fmt.Errorf("failed to read passphrase: %v", err)
	}
	utils.TryMlock(passwordConfirm)
	defer clear(passwordConfirm)

	if string(password) != string(passwordConfirm) {
		return fmt.Errorf("passphrases do not match")
	}

	encrypted, err := crypt.EncryptRawBytes([]byte(noteName), password)
	if err != nil {
		return err
	}

	fmt.Println(output)
	file, err := os.Create(output)
	if err != nil {
		return err
	}

	_, err = file.Write(encrypted)
	if err != nil {
		return err
	}

	file.Sync()
	return nil
}