types

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2024 License: MIT Imports: 3 Imported by: 0

README

Package types

The types package provides custom types for various purposes. Currently, it includes the Hash type for representing hashed strings.

Installation

To install the types package, use the following command:

go get -u github.com/thirathawat/types

Usage

Import the package into your Go program:

import "github.com/thirathawat/types"
Hash Type

The Hash type is a custom type based on the string type. It represents a hashed string value.

Creating a Hash

To create a Hash value, simply assign a string value to it:

var hashedPassword types.Hash = "mysecretpassword"
JSON Serialization

The Hash type implements the json.Marshaler and json.Unmarshaler interfaces, allowing you to easily serialize and deserialize JSON representations of the Hash value.

Example usage:

hashedPassword := types.Hash("mysecretpassword")

// Serialize to JSON
data, err := json.Marshal(hashedPassword)
if err != nil {
    log.Fatal(err)
}

// Deserialize from JSON
var restoredHash types.Hash
err = json.Unmarshal(data, &restoredHash)
if err != nil {
    log.Fatal(err)
}
String Representation

The Hash type also implements the fmt.Stringer interface, providing a string representation of the hashed value.

Example usage:

hashedPassword := types.Hash("mysecretpassword")

str := hashedPassword.String()
fmt.Println(str) // Output: "$2a$10$ldYn/9LioR9v/EsHf/6qQ.mtD7iDQwB58gUxCW.GgNt3G82Mu4Ebe"
Comparison

The Hash type provides a Compare method to compare the hashed value with a plain string.

Example usage:

hashedPassword := types.Hash("mysecretpassword")

isMatch := hashedPassword.Compare("mysecretpassword")
fmt.Println(isMatch) // Output: true

isMatch = hashedPassword.Compare("wrongpassword")
fmt.Println(isMatch) // Output: false

License

This package is distributed under the MIT License. See the LICENSE file for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hash

type Hash string

func (Hash) Compare

func (h Hash) Compare(plain string) bool

Compare compares the hashed value with a plain string.

func (Hash) MarshalBSON added in v1.0.3

func (h Hash) MarshalBSON() ([]byte, error)

MarshalBSON implements the bson.Marshaler interface.

func (Hash) MarshalJSON

func (h Hash) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Hash) String

func (h Hash) String() string

String implements the fmt.Stringer interface.

func (*Hash) UnmarshalBSON added in v1.0.3

func (h *Hash) UnmarshalBSON(data []byte) error

UnmarshalBSON implements the bson.Unmarshaler interface.

func (*Hash) UnmarshalJSON

func (h *Hash) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

Jump to

Keyboard shortcuts

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