Skip to content

Instantly share code, notes, and snippets.

@scottleedavis
Last active September 7, 2019 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottleedavis/cf07abde16e15d7abde990282869471a to your computer and use it in GitHub Desktop.
Save scottleedavis/cf07abde16e15d7abde990282869471a to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"bytes"
"fmt"
"gopkg.in/auyer/steganography.v2"
"image"
"os"
)
func main() {
inFile, err := os.Open("./assets/pre_test.png") // opening file
if err != nil {
fmt.Println("unable to open file")
}
defer inFile.Close()
reader := bufio.NewReader(inFile) // buffer reader
img, _, err := image.Decode(reader)
if err != nil {
fmt.Println("ERROR: original image is corrupt " + err.Error())
return
}
w := &bytes.Buffer{}
err = steganography.Encode(w, img, []byte("this is a watermark"))
if err != nil {
fmt.Println(err.Error())
}
//Save file
outFile, err := os.Create("test.png") // Creates file to write the message into
if err != nil {
fmt.Println("Error creating file %s: %v", "test.png", err)
}
w.WriteTo(outFile) // this is more concise
outFile.Close()
//Read file
inFile2, err := os.Open("test.png")
if err != nil {
fmt.Println("unable to open file")
}
defer inFile2.Close()
reader = bufio.NewReader(inFile2)
img, _, err = image.Decode(reader)
if err != nil {
fmt.Println("unable to decode file")
}
sizeOfMessage := steganography.GetMessageSizeFromImage(img)
msg := steganography.Decode(sizeOfMessage, img)
fmt.Println("**** " + string(msg))
}
@auyer
Copy link

auyer commented Sep 7, 2019

From like 50 to 53 you are repeating the ops in 45 to 48

	img, _, err = image.Decode(reader)
	if err != nil {
		fmt.Println("unable to decode file")
	}

	img, _, err = image.Decode(reader)  // This is the same as the above
	if err != nil {
		fmt.Println(err.Error())
	}

If works fine without the second block.

@auyer
Copy link

auyer commented Sep 7, 2019

Besides what I mentioned above, I would change a few things:

L 36 :

w.WriteTo(outFile) // this is more concise

L 37 : close the outFile

outFile.Close()

@scottleedavis
Copy link
Author

Awesome, thank you for the feedback. I made the changes you mention above, however the output is a blank message for me

$ go run test_infile.go 
**** 

@scottleedavis
Copy link
Author

updated with new png, and it works. must be the original png corrupted?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment