Documentation
¶
Overview ¶
Package enc provides an easy way to encode data in multiple commonly used formats such as Hex and Base64. It wraps the standard library packages such as encoding/base32 and encoding/base64 into easy to use functions.
Index ¶
- func Base32(p []byte) string
- func Base32Hex(p []byte) string
- func Base64(p []byte) string
- func Base64URL(p []byte) string
- func Hex(p []byte) string
- func JSON(v interface{}, opt ...Options) string
- func JSONformat(v interface{}, prefix, indent string) string
- func JSONhtml(v interface{}) string
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Base32 ¶
Base32 function converts the supplied data to standard base32 encoded string as per RFC 4648
Example ¶
package main import ( "fmt" "github.com/boseji/auth/enc" ) func main() { testInput := []byte("Silence is the divine secret to success ...") fmt.Println(enc.Base32(testInput)) }
Output: KNUWYZLOMNSSA2LTEB2GQZJAMRUXM2LOMUQHGZLDOJSXIIDUN4QHG5LDMNSXG4ZAFYXC4===
func Base32Hex ¶
Base32Hex function converts the supplied data to "Extended Hex Alphabet" based base32 encoded string as defined in RFC 4648
Example ¶
package main import ( "fmt" "github.com/boseji/auth/enc" ) func main() { testInput := []byte("Silence is the divine secret to success ...") fmt.Println(enc.Base32Hex(testInput)) }
Output: ADKMOPBECDII0QBJ41Q6GP90CHKNCQBECKG76PB3E9IN883KDSG76TB3CDIN6SP05ON2S===
func Base64 ¶
Base64 function converts the supplied data to a Base64 Standard encoding formatted string as defined in RFC 4648.
Example ¶
package main import ( "fmt" "github.com/boseji/auth/enc" ) func main() { testInput := []byte("Silence is the divine secret to success ...") fmt.Println(enc.Base64(testInput)) }
Output: U2lsZW5jZSBpcyB0aGUgZGl2aW5lIHNlY3JldCB0byBzdWNjZXNzIC4uLg==
func Base64URL ¶
Base64URL function converts the supplied data to a Base64 URL encoding formatted string as defined for URLs and file names.
Example ¶
package main import ( "fmt" "github.com/boseji/auth/enc" ) func main() { testInput := []byte("Silence is the divine secret to success ...") fmt.Println(enc.Base64URL(testInput)) }
Output: U2lsZW5jZSBpcyB0aGUgZGl2aW5lIHNlY3JldCB0byBzdWNjZXNzIC4uLg==
func Hex ¶
Hex function converts the supplied data to Hex encoded string it is in ways similar to fmt.Printf("%x", p)
Example ¶
package main import ( "fmt" "github.com/boseji/auth/enc" ) func main() { testInput := []byte("Silence is the divine secret to success ...") fmt.Println(enc.Hex(testInput)) }
Output: 53696c656e63652069732074686520646976696e652073656372657420746f2073756363657373202e2e2e
func JSON ¶
JSON function converts the supplied data to a JSON formated string. In case it fails to interpret the data it would return an empty string. This function also accepts functional options in a list that can change the encoded output.
Example ¶
package main import ( "fmt" "github.com/boseji/auth/enc" ) func main() { // Make sure the the structure fields are exported. Add meaningful JSON // names that match your target using tags. testInput := []struct { Name string `json:"name"` Age int `json:"ageOf"` }{ {"Mohit", 30}, {"Deepti", 22}, } fmt.Println(enc.JSON(testInput)) }
Output: [{"name":"Mohit","ageOf":30},{"name":"Deepti","ageOf":22}]
func JSONformat ¶
JSONformat function converts the supplied data to JSON formatted string and adds additional pretty print formatting using prefix & indent inputs.
Example ¶
package main import ( "fmt" "github.com/boseji/auth/enc" ) func main() { // Make sure the the structure fields are exported. Add meaningful JSON // names that match your target using tags. testInput := []struct { Name string `json:"name"` Age int `json:"ageOf"` }{ {"Keshav", 25}, {"Mohan", 15}, } fmt.Println(enc.JSONformat(testInput, "", "\t")) }
Output: [ { "name": "Keshav", "ageOf": 25 }, { "name": "Mohan", "ageOf": 15 } ]
func JSONhtml ¶
func JSONhtml(v interface{}) string
JSONhtml function converts the supplies data to JSON formated string and HTML Escapes it. This makes the string safe to be used embedded in HTML.
Example ¶
package main import ( "fmt" "github.com/boseji/auth/enc" ) func main() { testInput := []struct { Name string Message string }{ { Name: "The old Yoda", Message: "<h1>Speak last,<br>Show Respect,<br>power and Wisdom shall follow.</h1>", }, } fmt.Println(enc.JSONhtml(testInput)) }
Output: [{"Name":"The old Yoda","Message":"\u003ch1\u003eSpeak last,\u003cbr\u003eShow Respect,\u003cbr\u003epower and Wisdom shall follow.\u003c/h1\u003e"}]
Types ¶
type Options ¶
type Options func(*option) *option
Options is the functional options for JSON Encoding
func Format ¶
Format functional option provides a way to pretty format the JSON output
Example ¶
package main import ( "fmt" "github.com/boseji/auth/enc" ) func main() { // Make sure the the structure fields are exported. Add meaningful JSON // names that match your target using tags. testInput := []struct { Name string `json:"name"` Age int `json:"ageOf"` }{ {"Mohit", 30}, {"Deepti", 22}, } fmt.Println(enc.JSON(testInput, enc.Format("", "\t"))) }
Output: [ { "name": "Mohit", "ageOf": 30 }, { "name": "Deepti", "ageOf": 22 } ]
func HTMLEscaped ¶
func HTMLEscaped() Options
HTMLEscaped functional option enables HTML Escaping for JSON
Example ¶
package main import ( "fmt" "github.com/boseji/auth/enc" ) func main() { testInput := []struct { Name string Message string }{ { Name: "The old Yoda", Message: "<h1>Speak last,<br>Show Respect,<br>power and Wisdom shall follow.</h1>", }, } fmt.Println(enc.JSON(testInput, enc.HTMLEscaped())) }
Output: [{"Name":"The old Yoda","Message":"\u003ch1\u003eSpeak last,\u003cbr\u003eShow Respect,\u003cbr\u003epower and Wisdom shall follow.\u003c/h1\u003e"}]