passwd

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2023 License: MIT Imports: 5 Imported by: 0

README

passwd

GoDoc Go Report Card License

passwd is a zero-setup package for hashing passwords and comparing passwords. This package makes it easy to use passwd.Password just like a normal string, but it is secure.

Features

  • Zero-setup, just use passwd.Password like a normal string, and it will be hashed automatically when storing to the database.
  • Implements sql.Scanner and driver.Valuer interfaces.
  • Hide the password when printing and Marshaling to JSON.
  • Customizable hash comparer algorithm.

Installation

go get github.com/pkg-id/passwd

Usage

Here's an example of how to use passwd with a PostgreSQL database:

package main

import (
	"database/sql"
	"fmt"
	"log"

	"github.com/pkg-id/passwd"
	"github.com/pkg-id/passwd/bcrypt"

	_ "github.com/lib/pq"
)

func main() {
	// Open a connection to the database.
	db, err := sql.Open("postgres", "user=postgres password=postgres dbname=mydb sslmode=disable")
	if err != nil {
		log.Fatalf("open db. error: %v", err)
	}
	defer db.Close()

	// OPTIONAL: Set the hash comparer to bcrypt.
	passwd.SetHashComparer(bcrypt.DefaultCost)

	// Hash the password.
	plain := "pass1234"
	pwd := passwd.Password(plain)

	// Insert the password into the database.
	const insert = "INSERT INTO users(password) VALUES ($1) RETURNING id;"
	var id int64
	err = db.QueryRow(insert, pwd).Scan(&id)
	if err != nil {
		log.Fatalf("query row. error: %v", err)
	}

	// Retrieve the password from the database.
	const query = "SELECT password FROM users WHERE id = $1;"
	var scanned passwd.Password
	err = db.QueryRow(query, id).Scan(&scanned)
	if err != nil {
		log.Fatalf("scan row. error: %v", err)
	}

	// Compare the password.
	err = scanned.Compare(plain)
	if err != nil {
		log.Fatalf("expect password match")
	}

	err = scanned.Compare("must be not match")
	if err == nil {
		log.Fatalf("expect password not match")
	}
}

The passwd.SetHashComparer function is optional, since bcrypt is already used as the default hash comparer. However, it can be used to set a different hash comparer if needed.

How it works

passwd.Password is a new type based on the string type, and it is used to represent a password. When a password is stored, it is hashed using the default hash comparer (bcrypt). When a password is retrieved from the database, it is compared to the plain text password using the same hash comparer. If the passwords match, no error is returned. If the passwords do not match, an error is returned.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Overview

Package passwd provides a secure way to manage passwords in Go without requiring any additional setup. The Password type is used just like a normal string, but provides additional functionality such as hashing and comparing passwords securely.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetHashComparer

func SetHashComparer(hc HashComparer)

SetHashComparer sets the global hash comparer to the specified value. This function is concurrent-safe.

Types

type HashComparer

type HashComparer interface {
	// Hash generates a hash from the specified plaintext password using the configured algorithm.
	// It returns the resulting hash as a string and any errors that occur during the hash generation.
	Hash(plain string) (string, error)

	// Compare compares the specified plaintext password with the specified hash.
	// It returns an error if the comparison fails.
	Compare(hash string, plain string) error
}

HashComparer is a contract for the hashing algorithm that can generate and compare hashes. To provide a custom implementation use the SetHashComparer function.

type Password

type Password string

Password is a type that represents a password. It provides additional functionality for securely hashing and comparing passwords.

func (Password) Compare

func (p Password) Compare(plain string) error

Compare compares the password with plain text. It returns an error if the comparison fails.

func (Password) MarshalJSON

func (p Password) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the password. It hides the actual password value by returning "FILTERED".

func (*Password) Scan

func (p *Password) Scan(src any) error

Scan implements the sql.Scanner interface. It sets the password value to an empty string if the source value is nil. Otherwise, it sets the password value to the source value.

func (Password) String

func (p Password) String() string

String returns a string representation of the password. It hides the actual password value by returning "FILTERED".

func (Password) Value

func (p Password) Value() (driver.Value, error)

Value implements the driver.Valuer interface. It generates a hash from the password and returns the hash value. It returns an error if the hash generation fails.

Directories

Path Synopsis
Package bcrypt provides an implementation of the HashComparer interface using the bcrypt algorithm.
Package bcrypt provides an implementation of the HashComparer interface using the bcrypt algorithm.

Jump to

Keyboard shortcuts

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