change types
This commit is contained in:
parent
454820f6d6
commit
2ee034250e
9 changed files with 43 additions and 36 deletions
|
@ -9,11 +9,11 @@ import (
|
|||
"filippo.io/age/armor"
|
||||
)
|
||||
|
||||
func (b *Bloat[T]) decrypt(src []byte) ([]byte, error) {
|
||||
func (b *BloatEnc[T]) decrypt(src []byte) ([]byte, error) {
|
||||
|
||||
input := bytes.NewReader(src)
|
||||
deArmored := armor.NewReader(input)
|
||||
out, err := age.Decrypt(deArmored, &b.Enc)
|
||||
out, err := age.Decrypt(deArmored, &b.EncPriv)
|
||||
if err != nil {
|
||||
fmt.Println("decrypt error")
|
||||
fmt.Println(err)
|
||||
|
|
|
@ -8,11 +8,11 @@ import (
|
|||
"filippo.io/age/armor"
|
||||
)
|
||||
|
||||
func (b *Bloat[T]) encrypt(plain []byte) ([]byte, error) {
|
||||
func (b *BloatEnc[T]) encrypt(plain []byte) ([]byte, error) {
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
armorWriter := armor.NewWriter(buf)
|
||||
w, err := age.Encrypt(armorWriter, b.Enc.Recipient()) // my wrapper doesn't support multiple recipients yet
|
||||
w, err := age.Encrypt(armorWriter, &b.EncPub) // my wrapper doesn't support multiple recipients yet
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
"fmt"
|
||||
)
|
||||
|
||||
func (b *Bloat[T]) marshal(body T) ([]byte, error) {
|
||||
func (b *BloatPlain[T]) marshal(body T) ([]byte, error) {
|
||||
final, err := xml.Marshal(body)
|
||||
if err != nil {
|
||||
fmt.Println("marshal error")
|
||||
|
|
49
obj.go
49
obj.go
|
@ -8,32 +8,32 @@ import (
|
|||
"filippo.io/age"
|
||||
)
|
||||
|
||||
type Bloat[T any] struct {
|
||||
SignPub minisign.PublicKey
|
||||
signPriv minisign.PrivateKey
|
||||
RequireEnc bool
|
||||
Enc age.X25519Identity
|
||||
// EncPub age.X25519Recipient
|
||||
// encPriv age.X25519Identity // Scrypt is the password one?
|
||||
type BloatPlain[T any] struct {
|
||||
SignPub minisign.PublicKey
|
||||
SignPriv minisign.PrivateKey
|
||||
}
|
||||
type BloatEnc[T any] struct {
|
||||
BloatPlain[T]
|
||||
EncPub age.X25519Recipient
|
||||
EncPriv age.X25519Identity // Scrypt is the password one?
|
||||
}
|
||||
|
||||
func NewPlainBloat[T any]() (Bloat[T], error) {
|
||||
func NewPlainBloat[T any]() (BloatPlain[T], error) {
|
||||
pub, sign, err := minisign.GenerateKey(rand.Reader)
|
||||
if err != nil {
|
||||
fmt.Println("problem generating minisign")
|
||||
fmt.Println(err)
|
||||
return Bloat[T]{}, err
|
||||
return BloatPlain[T]{}, err
|
||||
}
|
||||
res := Bloat[T]{
|
||||
SignPub: pub,
|
||||
signPriv: sign,
|
||||
RequireEnc: false,
|
||||
res := BloatPlain[T]{
|
||||
SignPub: pub,
|
||||
SignPriv: sign,
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
func NewEncBloat[T any]() (Bloat[T], error) {
|
||||
res, err := NewPlainBloat[T]()
|
||||
res.RequireEnc = true
|
||||
func NewEncBloat[T any]() (BloatEnc[T], error) {
|
||||
var res BloatEnc[T]
|
||||
pubres, err := NewPlainBloat[T]()
|
||||
if err != nil {
|
||||
fmt.Println("problem generating signing part")
|
||||
}
|
||||
|
@ -41,13 +41,20 @@ func NewEncBloat[T any]() (Bloat[T], error) {
|
|||
if err != nil {
|
||||
fmt.Println("unable to generate")
|
||||
fmt.Println(err)
|
||||
return Bloat[T]{}, err
|
||||
return BloatEnc[T]{}, err
|
||||
}
|
||||
// recip := enc.Recipient()
|
||||
res.Enc = *enc
|
||||
// res.encPriv = *enc
|
||||
// res.EncPub = *recip
|
||||
res.SignPub = pubres.SignPub
|
||||
res.SignPriv = pubres.SignPriv
|
||||
|
||||
res.EncPriv = *enc
|
||||
res.EncPub = *enc.Recipient()
|
||||
// fmt.Printf("made age key: %s\n", enc.Recipient().String())
|
||||
return res, nil
|
||||
|
||||
}
|
||||
func plainFromEnc[T any](p BloatEnc[T]) BloatPlain[T] {
|
||||
var r BloatPlain[T]
|
||||
r.SignPriv = p.SignPriv
|
||||
r.SignPub = p.SignPub
|
||||
return r
|
||||
}
|
||||
|
|
6
read.go
6
read.go
|
@ -5,7 +5,7 @@ import (
|
|||
"fmt"
|
||||
)
|
||||
|
||||
func (b *Bloat[T]) ReadEnc(packed []byte, author Bloat[T]) (T, error) {
|
||||
func (b *BloatEnc[T]) ReadEnc(packed []byte, author BloatEnc[T]) (T, error) {
|
||||
var final T
|
||||
decrypted, err := b.decrypt(packed) // our own key
|
||||
if err != nil {
|
||||
|
@ -13,10 +13,10 @@ func (b *Bloat[T]) ReadEnc(packed []byte, author Bloat[T]) (T, error) {
|
|||
fmt.Println(err)
|
||||
return final, err
|
||||
}
|
||||
final, err = b.ReadPlain(decrypted, author)
|
||||
final, err = b.ReadPlain(decrypted, BloatPlain[T](plainFromEnc(author)))
|
||||
return final, err
|
||||
}
|
||||
func (b *Bloat[T]) ReadPlain(plain []byte, author Bloat[T]) (T, error) {
|
||||
func (b *BloatPlain[T]) ReadPlain(plain []byte, author BloatPlain[T]) (T, error) {
|
||||
var final T
|
||||
body, verified, err := author.verify(plain) // author's key
|
||||
if err != nil {
|
||||
|
|
4
sign.go
4
sign.go
|
@ -7,9 +7,9 @@ import (
|
|||
)
|
||||
|
||||
// sign the payload and append 4 lines
|
||||
func (b *Bloat[T]) sign(plaintext []byte) ([]byte, error) {
|
||||
func (b *BloatPlain[T]) sign(plaintext []byte) ([]byte, error) {
|
||||
|
||||
signature := minisign.Sign(b.signPriv, plaintext)
|
||||
signature := minisign.Sign(b.SignPriv, plaintext)
|
||||
parts := [][]byte{plaintext, signature}
|
||||
final := bytes.Join(parts, []byte("\n"))
|
||||
return final, nil
|
||||
|
|
|
@ -15,7 +15,7 @@ types to decode:
|
|||
|
||||
*/
|
||||
|
||||
func (b *Bloat[T]) unmarshal(plain []byte) (T, error) {
|
||||
func (b *BloatPlain[T]) unmarshal(plain []byte) (T, error) {
|
||||
var res T
|
||||
err := xml.Unmarshal(plain, &res)
|
||||
if err != nil {
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
)
|
||||
|
||||
// Verify checks the minisign signature and returns the body if valid.
|
||||
func (b *Bloat[T]) verify(message []byte) ([]byte, bool, error) {
|
||||
func (b *BloatPlain[T]) verify(message []byte) ([]byte, bool, error) {
|
||||
// signature is last 4 lines:
|
||||
separated := bytes.Split(message, []byte("\n"))
|
||||
signature := bytes.Join(separated[len(separated)-5:len(separated)], []byte("\n"))
|
||||
|
|
6
write.go
6
write.go
|
@ -2,7 +2,7 @@ package bloat
|
|||
|
||||
import "fmt"
|
||||
|
||||
func (b *Bloat[T]) WritePlain(body T, recip Bloat[T]) ([]byte, error) {
|
||||
func (b *BloatPlain[T]) WritePlain(body T, recip BloatPlain[T]) ([]byte, error) {
|
||||
start, err := b.marshal(body)
|
||||
if err != nil {
|
||||
fmt.Println("marshal failed")
|
||||
|
@ -17,8 +17,8 @@ func (b *Bloat[T]) WritePlain(body T, recip Bloat[T]) ([]byte, error) {
|
|||
}
|
||||
return signed, err
|
||||
}
|
||||
func (b *Bloat[T]) WriteEnc(body T, recip Bloat[T]) ([]byte, error) {
|
||||
signed, err := b.WritePlain(body, recip)
|
||||
func (b *BloatEnc[T]) WriteEnc(body T, recip BloatEnc[T]) ([]byte, error) {
|
||||
signed, err := b.WritePlain(body, BloatPlain[T](plainFromEnc(recip)))
|
||||
if err != nil {
|
||||
fmt.Println("plain write failed")
|
||||
fmt.Println(err)
|
||||
|
|
Loading…
Add table
Reference in a new issue