Skip to content

Commit 1d80293

Browse files
committed
Fix not writing chunk hashes to file; Make payment hash fixed size
1 parent 26b5ff4 commit 1d80293

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

example/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func decrypt(inPath, outPath, preimage string, chunkSize int64) error {
109109
}
110110

111111
root := fmt.Sprintf("%x", proof.MerkleProof.Root)
112-
log.Println("Generated proof", "proof root", root)
112+
log.Println("Generated proof", "root", root)
113113
}
114114

115115
return err

pkg/decrypt.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99

1010
// Decrypt tries to decrypt file with given preimage and returns io.Reader of decrypted file.
1111
//
12-
// In case of an error, decrypt returns chunk index alongisde the error.
12+
// In case of an error, decrypt returns chunk index alongside the error.
1313
//
14-
// If chunk index is not -1, then error ocurred while decrypting the chunk
14+
// If chunk index is not -1, then error occurred while decrypting the chunk
1515
// hence proof for given chunk should be generated.
1616
func Decrypt(preimage []byte, file io.ReadSeeker, chunkSize int64) (io.Reader, int, error) {
1717
var out bytes.Buffer
@@ -65,9 +65,8 @@ func Decrypt(preimage []byte, file io.ReadSeeker, chunkSize int64) (io.Reader, i
6565
h := sha256.New()
6666
h.Write(decryptedChunk)
6767
hash := h.Sum(nil)
68-
6968
if !bytes.Equal(expectedHash, hash) {
70-
return nil, int(i), fmt.Errorf("failed to decrypt file")
69+
return nil, int(i), fmt.Errorf("failed to decrypt chunk %d. expected %x got %x", i, expectedHash, hash)
7170
}
7271

7372
out.Write(decryptedChunk)
@@ -77,5 +76,5 @@ func Decrypt(preimage []byte, file io.ReadSeeker, chunkSize int64) (io.Reader, i
7776
i++
7877
}
7978

80-
return nil, 0, nil
79+
return io.Reader(&out), 0, nil
8180
}

pkg/encrypt.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bitstream
22

33
import (
44
"bytes"
5+
"crypto/sha256"
56
"fmt"
67
"io"
78

@@ -32,7 +33,9 @@ func Encrypt(pk *btcec.PrivateKey, paymentHash, preimage []byte, inFile io.ReadS
3233
return fmt.Errorf("failed to write schnorr sig: %w", err)
3334
}
3435

35-
_, err = outFile.Write(paymentHash)
36+
var paymentHashFixed [32]byte
37+
copy(paymentHashFixed[:], paymentHash)
38+
_, err = outFile.Write(paymentHashFixed[:])
3639
if err != nil {
3740
return fmt.Errorf("failed to write payment hash: %w", err)
3841
}
@@ -68,7 +71,13 @@ func EncryptFile(preimage []byte, file io.ReadSeeker, chunkSize int64) (Encrypte
6871
return nil, nil, err
6972
}
7073

74+
h := sha256.New()
75+
h.Write(chunk)
76+
hash := h.Sum(nil)
77+
7178
encryptedChunk := ChunkCipher(i, preimage, chunk)
79+
80+
b.Write(hash)
7281
b.Write(encryptedChunk)
7382

7483
encNode := NewNode(encryptedChunk)

0 commit comments

Comments
 (0)