dec

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 1, 2021 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package dec provides an easy way to decide data from 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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base32

func Base32(s string) ([]byte, error)

Base32 function decodes the supplied standard base32 encoded string back to its source data.

Example
package main

import (
	"fmt"

	"github.com/boseji/auth/dec"
)

func main() {

	testInput := "KNUWYZLOMNSSA2LTEB2GQZJAMRUXM2LOMUQHGZLDOJSXIIDUN4QHG5LDMNSXG4ZAFYXC4==="

	buf, err := dec.Base32(testInput)
	if err != nil {
		panic(err.Error())
	}

	fmt.Printf("%q", string(buf))

}
Output:

"Silence is the divine secret to success ..."

func Base32Hex

func Base32Hex(s string) ([]byte, error)

Base32Hex function decodes the supplied "Extended Hex Alphabet" based base32 encoded string back to its source data.

Example
package main

import (
	"fmt"

	"github.com/boseji/auth/dec"
)

func main() {

	testInput := "ADKMOPBECDII0QBJ41Q6GP90CHKNCQBECKG76PB3E9IN883KDSG76TB3CDIN6SP05ON2S==="

	buf, err := dec.Base32Hex(testInput)
	if err != nil {
		panic(err.Error())
	}

	fmt.Printf("%q", string(buf))

}
Output:

"Silence is the divine secret to success ..."

func Base64

func Base64(s string) ([]byte, error)

Base64 function decodes the supplied Base64 Standard encoding formatted string to its source data.

Example
package main

import (
	"fmt"

	"github.com/boseji/auth/dec"
)

func main() {

	testInput := "U2lsZW5jZSBpcyB0aGUgZGl2aW5lIHNlY3JldCB0byBzdWNjZXNzIC4uLg=="

	buf, err := dec.Base64(testInput)
	if err != nil {
		panic(err.Error())
	}

	fmt.Printf("%q", string(buf))

}
Output:

"Silence is the divine secret to success ..."

func Base64URL

func Base64URL(s string) ([]byte, error)

Base64URL function decodes the Base64 URL encoded string to its source data.

Example
package main

import (
	"fmt"

	"github.com/boseji/auth/dec"
)

func main() {

	testInput := "U2lsZW5jZSBpcyB0aGUgZGl2aW5lIHNlY3JldCB0byBzdWNjZXNzIC4uLg=="

	buf, err := dec.Base64URL(testInput)
	if err != nil {
		panic(err.Error())
	}

	fmt.Printf("%q", string(buf))

}
Output:

"Silence is the divine secret to success ..."

func Hex

func Hex(s string) ([]byte, error)

Hex function decodes the supplied Hex encoded string back to its source data

Example
package main

import (
	"fmt"

	"github.com/boseji/auth/dec"
)

func main() {

	testInput := "53696c656e63652069732074686520646976696e652073656372657420746f2073756363657373202e2e2e"

	buf, err := dec.Hex(testInput)
	if err != nil {
		panic(err.Error())
	}

	fmt.Printf("%q", string(buf))

}
Output:

"Silence is the divine secret to success ..."

func JSON

func JSON(s string, result interface{}) error

JSON function decodes the supplied JSON string to fill result storage provided by reference. In case the size of reference in case of slice is not sufficient it would reallocated. The Reference provided here must be a pointer to the type of data representted in the JSON string. The decoded value is returned in the reference 'result' variable.

Example
package main

import (
	"fmt"

	"github.com/boseji/auth/dec"
)

func main() {

	testInput := `
	[
		{
			"name": "Keshav",
			"ageOf": 25
		},
		{
			"name": "Mohan",
			"ageOf": 15
		}
	]
	`

	// Make sure that supplied reference also has the correct fields
	// and JSON tags for the type. Also incase the data is an array or slice
	// the same needs to be created for filling data from JSON string.
	// In this case we are using a slice of struct of the corresponding
	// type.
	ref := []struct {
		Name string `json:"name"`
		Age  int    `json:"ageOf"`
	}{}

	// Note the use of Reference even in case of slice, struct, or array
	err := dec.JSON(testInput, &ref)
	if err != nil {
		panic(err.Error())
	}
	fmt.Println(ref)

}
Output:

[{Keshav 25} {Mohan 15}]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL