Skip to content

Commit d8e664f

Browse files
authored
Merge pull request #17 from cfromknecht/obfuscator-serialization
Obfuscator Serialization
2 parents 18bf661 + cd2eefa commit d8e664f

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

obfuscation.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ func (o *OnionErrorEncrypter) EncryptError(initial bool, data []byte) []byte {
6969
return onionEncrypt(o.sharedSecret, data)
7070
}
7171

72+
// Encode writes the encrypter's shared secret to the provided io.Writer.
73+
func (o *OnionErrorEncrypter) Encode(w io.Writer) error {
74+
_, err := w.Write(o.sharedSecret[:])
75+
return err
76+
}
77+
78+
// Decode restores the encrypter's share secret from the provided io.Reader.
79+
func (o *OnionErrorEncrypter) Decode(r io.Reader) error {
80+
_, err := io.ReadFull(r, o.sharedSecret[:])
81+
return err
82+
}
83+
7284
// Circuit is used encapsulate the data which is needed for data deobfuscation.
7385
type Circuit struct {
7486
// SessionKey is the key which have been used during generation of the

obfuscation_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sphinx
33
import (
44
"bytes"
55
"encoding/hex"
6+
"reflect"
67
"testing"
78

89
"github.com/roasbeef/btcd/btcec"
@@ -201,6 +202,21 @@ func TestOnionFailureSpecVector(t *testing.T) {
201202
sharedSecret: sharedSecrets[len(sharedSecrets)-1-i],
202203
}
203204

205+
var b bytes.Buffer
206+
if err := obfuscator.Encode(&b); err != nil {
207+
t.Fatalf("unable to encode obfuscator: %v", err)
208+
}
209+
210+
obfuscator2 := &OnionErrorEncrypter{}
211+
obfuscatorReader := bytes.NewReader(b.Bytes())
212+
if err := obfuscator2.Decode(obfuscatorReader); err != nil {
213+
t.Fatalf("unable to decode obfuscator: %v", err)
214+
}
215+
216+
if !reflect.DeepEqual(obfuscator, obfuscator2) {
217+
t.Fatalf("unable to reconstruct obfuscator: %v", err)
218+
}
219+
204220
if !bytes.Equal(expectedSharedSecret, obfuscator.sharedSecret[:]) {
205221
t.Fatalf("shared secret not match with spec: expected "+
206222
"%x, got %x", expectedSharedSecret,

0 commit comments

Comments
 (0)