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"
|
"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)
|
input := bytes.NewReader(src)
|
||||||
deArmored := armor.NewReader(input)
|
deArmored := armor.NewReader(input)
|
||||||
out, err := age.Decrypt(deArmored, &b.Enc)
|
out, err := age.Decrypt(deArmored, &b.EncPriv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("decrypt error")
|
fmt.Println("decrypt error")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|
|
@ -8,11 +8,11 @@ import (
|
||||||
"filippo.io/age/armor"
|
"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{}
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
armorWriter := armor.NewWriter(buf)
|
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 {
|
if err != nil {
|
||||||
return []byte{}, err
|
return []byte{}, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *Bloat[T]) marshal(body T) ([]byte, error) {
|
func (b *BloatPlain[T]) marshal(body T) ([]byte, error) {
|
||||||
final, err := xml.Marshal(body)
|
final, err := xml.Marshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("marshal error")
|
fmt.Println("marshal error")
|
||||||
|
|
45
obj.go
45
obj.go
|
@ -8,32 +8,32 @@ import (
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bloat[T any] struct {
|
type BloatPlain[T any] struct {
|
||||||
SignPub minisign.PublicKey
|
SignPub minisign.PublicKey
|
||||||
signPriv minisign.PrivateKey
|
SignPriv minisign.PrivateKey
|
||||||
RequireEnc bool
|
}
|
||||||
Enc age.X25519Identity
|
type BloatEnc[T any] struct {
|
||||||
// EncPub age.X25519Recipient
|
BloatPlain[T]
|
||||||
// encPriv age.X25519Identity // Scrypt is the password one?
|
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)
|
pub, sign, err := minisign.GenerateKey(rand.Reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("problem generating minisign")
|
fmt.Println("problem generating minisign")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return Bloat[T]{}, err
|
return BloatPlain[T]{}, err
|
||||||
}
|
}
|
||||||
res := Bloat[T]{
|
res := BloatPlain[T]{
|
||||||
SignPub: pub,
|
SignPub: pub,
|
||||||
signPriv: sign,
|
SignPriv: sign,
|
||||||
RequireEnc: false,
|
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
func NewEncBloat[T any]() (Bloat[T], error) {
|
func NewEncBloat[T any]() (BloatEnc[T], error) {
|
||||||
res, err := NewPlainBloat[T]()
|
var res BloatEnc[T]
|
||||||
res.RequireEnc = true
|
pubres, err := NewPlainBloat[T]()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("problem generating signing part")
|
fmt.Println("problem generating signing part")
|
||||||
}
|
}
|
||||||
|
@ -41,13 +41,20 @@ func NewEncBloat[T any]() (Bloat[T], error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("unable to generate")
|
fmt.Println("unable to generate")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return Bloat[T]{}, err
|
return BloatEnc[T]{}, err
|
||||||
}
|
}
|
||||||
// recip := enc.Recipient()
|
res.SignPub = pubres.SignPub
|
||||||
res.Enc = *enc
|
res.SignPriv = pubres.SignPriv
|
||||||
// res.encPriv = *enc
|
|
||||||
// res.EncPub = *recip
|
res.EncPriv = *enc
|
||||||
|
res.EncPub = *enc.Recipient()
|
||||||
// fmt.Printf("made age key: %s\n", enc.Recipient().String())
|
// fmt.Printf("made age key: %s\n", enc.Recipient().String())
|
||||||
return res, nil
|
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"
|
"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
|
var final T
|
||||||
decrypted, err := b.decrypt(packed) // our own key
|
decrypted, err := b.decrypt(packed) // our own key
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -13,10 +13,10 @@ func (b *Bloat[T]) ReadEnc(packed []byte, author Bloat[T]) (T, error) {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return final, err
|
return final, err
|
||||||
}
|
}
|
||||||
final, err = b.ReadPlain(decrypted, author)
|
final, err = b.ReadPlain(decrypted, BloatPlain[T](plainFromEnc(author)))
|
||||||
return final, err
|
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
|
var final T
|
||||||
body, verified, err := author.verify(plain) // author's key
|
body, verified, err := author.verify(plain) // author's key
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
4
sign.go
4
sign.go
|
@ -7,9 +7,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// sign the payload and append 4 lines
|
// 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}
|
parts := [][]byte{plaintext, signature}
|
||||||
final := bytes.Join(parts, []byte("\n"))
|
final := bytes.Join(parts, []byte("\n"))
|
||||||
return final, nil
|
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
|
var res T
|
||||||
err := xml.Unmarshal(plain, &res)
|
err := xml.Unmarshal(plain, &res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Verify checks the minisign signature and returns the body if valid.
|
// 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:
|
// signature is last 4 lines:
|
||||||
separated := bytes.Split(message, []byte("\n"))
|
separated := bytes.Split(message, []byte("\n"))
|
||||||
signature := bytes.Join(separated[len(separated)-5:len(separated)], []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"
|
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)
|
start, err := b.marshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("marshal failed")
|
fmt.Println("marshal failed")
|
||||||
|
@ -17,8 +17,8 @@ func (b *Bloat[T]) WritePlain(body T, recip Bloat[T]) ([]byte, error) {
|
||||||
}
|
}
|
||||||
return signed, err
|
return signed, err
|
||||||
}
|
}
|
||||||
func (b *Bloat[T]) WriteEnc(body T, recip Bloat[T]) ([]byte, error) {
|
func (b *BloatEnc[T]) WriteEnc(body T, recip BloatEnc[T]) ([]byte, error) {
|
||||||
signed, err := b.WritePlain(body, recip)
|
signed, err := b.WritePlain(body, BloatPlain[T](plainFromEnc(recip)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("plain write failed")
|
fmt.Println("plain write failed")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|
Loading…
Add table
Reference in a new issue