Change storing of the data to use multiple chunks

Data needs to be stored in multiple chunks with a maximum size of 1MB in order for image viewers to still handle the files. Especially libpng had problems with too large chunks sizes.
develop
trivernis 5 years ago
parent d340e93700
commit 7d274257bc

@ -14,6 +14,7 @@ import (
"log" "log"
"os" "os"
"syscall" "syscall"
"math"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
) )
@ -25,6 +26,7 @@ func check(err error) {
} }
const chunkName = "crPt" const chunkName = "crPt"
const chunkSize = 0x100000
var inputFile string var inputFile string
var outputFile string var outputFile string
@ -69,8 +71,13 @@ func EncryptDataPng(f *os.File, fin *os.File, fout *os.File) {
check(err) check(err)
inputData, err = encryptData(inputData) inputData, err = encryptData(inputData)
check(err) check(err)
cryptChunk := CreateChunk(inputData, chunkName) chunkCount := int(math.Ceil(float64(len(inputData)) / chunkSize))
png.AddMetaChunk(cryptChunk) for i := 0; i < chunkCount; i++ {
dataStart := i * chunkSize
dataEnd := dataStart + int(math.Min(chunkSize, float64(len(inputData[dataStart:]))))
cryptChunk := CreateChunk(inputData[dataStart:dataEnd], chunkName)
png.AddMetaChunk(cryptChunk)
}
err = png.Write(fout) err = png.Write(fout)
check(err) check(err)
} }
@ -80,9 +87,12 @@ func DecryptDataPng(f *os.File, fout *os.File) {
png := PngData{} png := PngData{}
err := png.Read(f) err := png.Read(f)
check(err) check(err)
cryptChunk := png.GetChunk(chunkName) var data []byte
if cryptChunk != nil { for _, cryptChunk := range png.GetChunksByName(chunkName) {
data, err := decryptData(cryptChunk.data) data = append(data, cryptChunk.data...)
}
if len(data) > 0 {
data, err = decryptData(data)
if err != nil { if err != nil {
log.Println("\nThe provided password is probably incorrect.") log.Println("\nThe provided password is probably incorrect.")
} }

@ -110,6 +110,17 @@ func (p *PngData) GetChunk(name string) *ChunkData {
return nil return nil
} }
// returns all chunks with a given name
func (p *PngData) GetChunksByName(name string) []ChunkData {
var chunks []ChunkData
for _, chunk := range p.chunks {
if chunk.name == name {
chunks = append(chunks, chunk)
}
}
return chunks
}
// validates the png by reading the header of the file // validates the png by reading the header of the file
func ValidatePng(f *os.File) (bool, []byte) { func ValidatePng(f *os.File) (bool, []byte) {
headerBytes := make([]byte, 8) headerBytes := make([]byte, 8)

Loading…
Cancel
Save