password

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 21 Imported by: 0

README

密码加解密

算法列表

算法名 特点 用途
Bcrypt 基于 Blowfish 算法,内置盐值,支持调整计算成本。 密码存储,防止暴力破解。
Argon2 内存硬性哈希算法,支持多种参数调整(内存、时间、并行度)。 码存储,适合高安全性需求。
PBKDF2 基于 HMAC 的密钥派生函数,支持多种哈希算法。 密码存储,兼容性好。
SHA-256/SHA-512 快速单向哈希算法,无内置盐值。 数据完整性校验,需手动添加盐值用于密码存储。
AES (Advanced Encryption Standard) 对称加密算法,支持 128/192/256 位密钥。 数据加密传输或存储。
RSA 非对称加密算法,基于大整数分解难题。 数据加密、数字签名。
ECDSA/ECDH (椭圆曲线算法) 基于椭圆曲线的非对称加密,密钥更短但安全性高。 数据完整性和认证。
HMAC (Hash-based Message Authentication Code) 基于哈希算法的消息认证码。 数据完整性和认证。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Argon2Crypto

type Argon2Crypto struct {
	// 参数可配置,默认使用推荐值
	Memory      uint32
	Iterations  uint32
	Parallelism uint8
	SaltLength  uint32
	KeyLength   uint32
}

Argon2Crypto 实现 Argon2id 密码哈希算法

func NewArgon2Crypto

func NewArgon2Crypto() *Argon2Crypto

NewArgon2Crypto 创建带默认参数的 Argon2 加密器

func (*Argon2Crypto) Encrypt

func (a *Argon2Crypto) Encrypt(password string) (string, error)

Encrypt 实现密码加密

func (*Argon2Crypto) Verify

func (a *Argon2Crypto) Verify(password, encrypted string) (bool, error)

Verify 验证密码

type BCryptCrypto

type BCryptCrypto struct{}

func NewBCryptCrypto

func NewBCryptCrypto() *BCryptCrypto

func (*BCryptCrypto) Encrypt

func (b *BCryptCrypto) Encrypt(password string) (encrypted string, err error)

Encrypt 使用 bcrypt 加密密码,返回加密后的字符串和空盐值

func (*BCryptCrypto) Verify

func (b *BCryptCrypto) Verify(password, encrypted string) (bool, error)

Verify 验证密码是否匹配加密后的字符串

type Crypto

type Crypto interface {
	// Encrypt 加密密码,返回加密后的字符串(包含算法标识和盐值)
	Encrypt(plainPassword string) (encrypted string, err error)

	// Verify 验证密码是否匹配
	Verify(plainPassword, encrypted string) (bool, error)
}

Crypto 密码加解密接口

func CreateCrypto

func CreateCrypto(algorithm string) (Crypto, error)

type ECDHCrypto

type ECDHCrypto struct {
	// contains filtered or unexported fields
}

ECDHCrypto 实现基于 ECDH 的密钥交换

func NewECDHCrypto

func NewECDHCrypto() (*ECDHCrypto, error)

NewECDHCrypto 创建一个新的 ECDHCrypto 实例

func (*ECDHCrypto) DeriveSharedSecret

func (e *ECDHCrypto) DeriveSharedSecret(publicKey string) ([]byte, error)

func (*ECDHCrypto) Encrypt

func (e *ECDHCrypto) Encrypt(plainPassword string) (string, error)

Encrypt 返回公钥作为加密结果

func (*ECDHCrypto) Verify

func (e *ECDHCrypto) Verify(plainPassword, encrypted string) (bool, error)

Verify 验证共享密钥是否一致

type ECDSACrypto

type ECDSACrypto struct {
	// contains filtered or unexported fields
}

ECDSACrypto 实现基于 ECDSA 的加密和验证

func NewECDSACrypto

func NewECDSACrypto() (*ECDSACrypto, error)

NewECDSACrypto 创建一个新的 ECDSACrypto 实例

func (*ECDSACrypto) Encrypt

func (e *ECDSACrypto) Encrypt(plainPassword string) (string, error)

Encrypt 使用 ECDSA 对消息进行签名

func (*ECDSACrypto) Verify

func (e *ECDSACrypto) Verify(plainPassword, encrypted string) (bool, error)

Verify 验证消息的签名是否有效

type HMACCrypto

type HMACCrypto struct {
	// contains filtered or unexported fields
}

HMACCrypto 实现基于 HMAC 的加密和验证

func NewHMACCrypto

func NewHMACCrypto(secretKey string) *HMACCrypto

NewHMACCrypto 创建一个新的 HMACCrypto 实例

func (*HMACCrypto) Encrypt

func (h *HMACCrypto) Encrypt(data string) (string, error)

Encrypt 使用 HMAC-SHA256 对数据进行加密

func (*HMACCrypto) Verify

func (h *HMACCrypto) Verify(data, encrypted string) (bool, error)

Verify 验证数据的 HMAC 值是否匹配

type PBKDF2Crypto

type PBKDF2Crypto struct {
	// 可配置参数,默认使用推荐值
	Iterations int
	KeyLength  int
	Hash       func() hash.Hash
	HashName   string
}

PBKDF2Crypto 实现 PBKDF2-HMAC 密码哈希算法

func NewPBKDF2Crypto

func NewPBKDF2Crypto() *PBKDF2Crypto

NewPBKDF2Crypto 创建带默认参数的 PBKDF2 加密器 (SHA256)

func NewPBKDF2WithSHA512

func NewPBKDF2WithSHA512() *PBKDF2Crypto

NewPBKDF2WithSHA512 创建使用 SHA512 的 PBKDF2 加密器

func (*PBKDF2Crypto) Encrypt

func (p *PBKDF2Crypto) Encrypt(password string) (string, error)

Encrypt 实现密码加密

func (*PBKDF2Crypto) Verify

func (p *PBKDF2Crypto) Verify(password, encrypted string) (bool, error)

Verify 验证密码

type RSACrypto

type RSACrypto struct {
	// contains filtered or unexported fields
}

RSACrypto 实现 RSA 加密和解密

func NewRSACrypto

func NewRSACrypto(keySize int) (*RSACrypto, error)

NewRSACrypto 创建一个新的 RSACrypto 实例

func (*RSACrypto) Decrypt

func (r *RSACrypto) Decrypt(encryptedData string) (string, error)

Decrypt 使用私钥解密数据

func (*RSACrypto) Encrypt

func (r *RSACrypto) Encrypt(data string) (string, error)

Encrypt 使用公钥加密数据

func (*RSACrypto) ExportPrivateKey

func (r *RSACrypto) ExportPrivateKey() (string, error)

ExportPrivateKey 导出私钥为 PEM 格式

func (*RSACrypto) ExportPublicKey

func (r *RSACrypto) ExportPublicKey() (string, error)

ExportPublicKey 导出公钥为 PEM 格式

type SHACrypto

type SHACrypto struct {
	Hash       func() hash.Hash
	HashName   string
	SaltLength int
}

SHACrypto 实现 SHA-256/SHA-512 密码哈希算法(带盐值)

func NewSHA256Crypto

func NewSHA256Crypto() *SHACrypto

NewSHA256Crypto 创建 SHA-256 加密器

func NewSHA512Crypto

func NewSHA512Crypto() *SHACrypto

NewSHA512Crypto 创建 SHA-512 加密器

func (*SHACrypto) Encrypt

func (s *SHACrypto) Encrypt(password string) (string, error)

Encrypt 实现密码加密(带盐值)

func (*SHACrypto) Verify

func (s *SHACrypto) Verify(password, encrypted string) (bool, error)

Verify 验证密码

Jump to

Keyboard shortcuts

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