Golang : Embed secret text string into binary(executable) file
Problem:
You want to hide certain plain text string that you plan to embed(include) in your executable or binary file. There are few reasons to do this, such as you don't want to the plain text information to be viewed from a hex editor, cat
or more
command. How to hide a plain text string that you plan to embed?
Solution:
Use the encoding/hex
package EncodeToString()
function to convert plain text string into hexadecimal value and use it in your code as constant. The code below will demonstrate how to read a plain text string from a file and convert it with EncodeToString()
function before converting it back to plain text with DecodeString()
function.
The code is only an example, in your production code, you need to embed the hexadecimal value as constant.
package main
import (
"encoding/hex"
"fmt"
"io/ioutil"
"os"
)
func ConvertFileToHexString(filepath string) (string, error) {
byteString, err := ioutil.ReadFile(filepath)
if err != nil {
return "", err
}
return hex.EncodeToString(byteString), nil
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage : %s <filename>\n", os.Args[0])
os.Exit(0)
}
filename := os.Args[1]
hexDataString, err := ConvertFileToHexString(filename)
// will display
// 746869732069732061207365637265742066696c652077697468207365637265742064617461210a
// for this example string "this is a secret file with secret data!"
//fmt.Println(hexDataString)
if err != nil {
panic(err)
}
bytesDataString, err := hex.DecodeString(hexDataString)
if err != nil {
panic(err)
}
fmt.Println("This is generated from reading : ", filename)
fmt.Print(string(bytesDataString))
// ok, we translated the data from file, but that's not what we want to achieve.
// we want to embed the data inside this executable.
// we will assign a constant with a slightly different message to indicate that it is NOT from file
const secretMessageInsideExecutable = "746869732069732061207365637265742066696c652077697468207365637265742064617461210a"
bytesDataString2, _ := hex.DecodeString(secretMessageInsideExecutable)
fmt.Println("------------------------------------")
fmt.Println("This is generated from embedded hexadecimal string found inside this executable")
fmt.Println(string(bytesDataString2))
}
Sample output:
secret.txt
file content:
this is a secret text message from a file!
$ ./embedtext secret.txt
This is generated from reading : secret.txt
this is a secret text message from a file!
------------------------------------
This is generated from embedded hexadecimal string found inside this executable
this is a secret file with secret data!
References:
http://stackoverflow.com/questions/17796043/embedding-text-file-into-compiled-executable
See also : Golang : Embedded or data bundling example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+18.4k Golang : Set, Get and List environment variables
+5k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+11.5k Golang : Calculations using complex numbers example
+11.6k Golang : Verify Linux user password again before executing a program example
+7.4k Golang : How to handle file size larger than available memory panic issue
+5.5k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+13.6k Generate salted password with OpenSSL example
+36.3k Golang : Validate IP address
+33.9k Golang : Create x509 certificate, private and public keys
+14.4k Golang : Send email with attachment(RFC2822) using Gmail API example
+9.6k Golang : Format strings to SEO friendly URL example
+13.2k Golang : Generate Code128 barcode