argon2

package module
v0.0.0-...-1d09f8d Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2024 License: MIT Imports: 9 Imported by: 0

README

argon2 - A Golang Package for Password Hashing with Argon2

Overview

argon2 is a Golang package that provides easy-to-use functions for hashing and verifying passwords using the Argon2id algorithm. Argon2 is a memory-hard password hashing algorithm designed to resist both GPU and side-channel attacks. This package ensures secure password storage and verification.

Features

  • Secure password hashing using Argon2id.
  • Automatic salt generation for each password hash.
  • Configurable parameters for memory, time, and parallelism.
  • Constant time comparison for password verification to prevent timing attacks.

Installation

To install the argon2 package, use the following command:

$ go get github.com/bionicosmos/argon2

Usage

Hashing a Password

To hash a password, simply call the Hash function with the password string:

package main

import (
    "fmt"

    "github.com/bionicosmos/argon2"
)

func main() {
    password := "B^G3cMzs0$14cH2201&&"
    hashedPassword := argon2.Hash(password)
    fmt.Println("Hashed Password:", hashedPassword)
    // Hashed Password: $argon2id$v=19$m=65536,t=1,p=8$EUfYfS8YbMh9bgBLTZE3Aw$l5XAX/80bljGYBvaHp3C1dt/x5pG5iG2clZahCun7hY
}
Verifying a Password

To verify a password against a hashed password, use the Verify function:

package main

import (
    "fmt"

    "github.com/bionicosmos/argon2"
)

func main() {
    password := "B^G3cMzs0$14cH2201&&"
    hashedPassword := "$argon2id$v=19$m=65536,t=1,p=8$EUfYfS8YbMh9bgBLTZE3Aw$l5XAX/80bljGYBvaHp3C1dt/x5pG5iG2clZahCun7hY"

    isValid := argon2.Verify(password, hashedPassword)
    fmt.Println("Password is valid:", isValid)
    // Password is valid: true
}

License

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


Feel free to contribute to the project by submitting issues or pull requests on the GitHub repository.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidEncodedHash indicates that the encoded hash is invalid.
	ErrInvalidEncodedHash = errors.New("invalid encoded hash")

	// ErrVersionMismatch indicates that there is a version mismatch between the
	// hash and the current Argon2 version.
	ErrVersionMismatch = errors.New("version mismatch")
)

Functions

func Hash

func Hash(password string) string

Hash returns the hash string from the given password. The function automatically generates a random salt and uses predefined parameters for the Argon2id algorithm.

func Stringify

func Stringify(key []byte, p *Param) string

func Verify

func Verify(password string, hash string) bool

Verify returns `true` if the password matches the hash, and `false` otherwise.

Types

type Param

type Param struct {
	Salt    []byte
	Time    uint32
	Memory  uint32
	Threads uint8
	KeyLen  uint32
}

func Parse

func Parse(s string) ([]byte, Param, error)

Jump to

Keyboard shortcuts

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