start implementing crypto handling
This commit is contained in:
56
crypto/crypto.go
Normal file
56
crypto/crypto.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"bytes"
|
||||
"io"
|
||||
"encoding/json"
|
||||
|
||||
"filippo.io/age"
|
||||
)
|
||||
|
||||
type Password struct {
|
||||
Service string `json:"Service"`
|
||||
Url string `json:"Url"`
|
||||
Username string `json:"Username"`
|
||||
Password string `json:"Password"`
|
||||
Tags []string `json:"Tags"`
|
||||
}
|
||||
|
||||
func (p *Password) ToJson() ([]byte, error) {
|
||||
return json.Marshal(p)
|
||||
}
|
||||
|
||||
func GetPasswordFromJson(b []byte) (Password, error) {
|
||||
var result Password
|
||||
err := json.Unmarshal(b, result)
|
||||
|
||||
if err != nil {
|
||||
return Password{}, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func Encrypt() {
|
||||
publicKey := "age1cy0su9fwf3gf9mw868g5yut09p6nytfmmnktexz2ya5uqg9vl9sss4euqm"
|
||||
recipient, err := age.ParseX25519Recipient(publicKey)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to parse public key %q: %v", publicKey, err)
|
||||
}
|
||||
|
||||
out := &bytes.Buffer{}
|
||||
|
||||
w, err := age.Encrypt(out, recipient)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create encrypted file: %v\n", err)
|
||||
}
|
||||
if _, err := io.WriteString(w, "Black lives matter."); err != nil {
|
||||
fmt.Printf("Failed to write to encrypted file: %v\n", err)
|
||||
}
|
||||
if err := w.Close(); err != nil {
|
||||
fmt.Printf("Failed to close encrypted file: %v\n", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Encrypted file size: %d\n", out.Len())
|
||||
}
|
||||
Reference in New Issue
Block a user