Replace readFileFull with ioutil.ReadAll

develop
trivernis 5 years ago
parent 621633263f
commit 66f3be84ad

1
.gitignore vendored

@ -1,4 +1,5 @@
*.png *.png
*.jpg *.jpg
*.txt *.txt
*.pdf
.idea .idea

@ -10,6 +10,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"os" "os"
) )
@ -20,6 +21,8 @@ func check(err error) {
} }
} }
const chunkName = "crPt"
var inputFile string var inputFile string
var outputFile string var outputFile string
var imageFile string var imageFile string
@ -34,8 +37,6 @@ func main() {
f, err := os.Open(imageFile) f, err := os.Open(imageFile)
check(err) check(err)
defer f.Close() defer f.Close()
info, _ := f.Stat()
fmt.Printf("size: %d\n",info.Size())
check(err) check(err)
fout, err := os.Create(outputFile) fout, err := os.Create(outputFile)
check(err) check(err)
@ -60,20 +61,22 @@ func EncryptDataPng(f *os.File, fin *os.File, fout *os.File) {
png := PngData{} png := PngData{}
err := png.Read(f) err := png.Read(f)
check(err) check(err)
inputData := readFileFull(fin) inputData, err := ioutil.ReadAll(fin)
check(err)
inputData, err = encryptData(inputData) inputData, err = encryptData(inputData)
check(err) check(err)
cryptChunk := CreateChunk(inputData, "crPt") cryptChunk := CreateChunk(inputData, chunkName)
png.AddMetaChunk(cryptChunk) png.AddMetaChunk(cryptChunk)
err = png.Write(fout) err = png.Write(fout)
check(err) check(err)
} }
// Decrypts the data from a png file
func DecryptDataPng(f *os.File, fout *os.File) { 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("crPt") cryptChunk := png.GetChunk(chunkName)
if cryptChunk != nil { if cryptChunk != nil {
data, err := decryptData(cryptChunk.data) data, err := decryptData(cryptChunk.data)
check(err) check(err)
@ -84,19 +87,6 @@ func DecryptDataPng(f *os.File, fout *os.File) {
} }
} }
// reads all bytes of a file
func readFileFull(f *os.File) []byte {
tmp := make([]byte, 8)
data := make([]byte, 0)
_, err := f.Read(tmp)
for err != io.EOF {
data = append(data, tmp...)
_, err = f.Read(tmp)
}
data = append(data, tmp...)
return data
}
// creates an encrypted png chunk // creates an encrypted png chunk
func encryptData(data []byte) ([]byte, error) { func encryptData(data []byte) ([]byte, error) {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)

Loading…
Cancel
Save