pbes2

package
v1.0.4032 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AddCipher = pbes2.AddCipher
	GetCipher = pbes2.GetCipher

	// 帮助函数
	GetCipherFromName   = pbes2.GetCipherFromName
	CheckCipherFromName = pbes2.CheckCipherFromName
	GetCipherName       = pbes2.GetCipherName
	CheckCipher         = pbes2.CheckCipher
)
View Source
var (
	DESCBC     = pbes2.DESCBC
	DESEDE3CBC = pbes2.DESEDE3CBC

	RC2CBC     = pbes2.RC2CBC
	RC2_40CBC  = pbes2.RC2_40CBC
	RC2_64CBC  = pbes2.RC2_64CBC
	RC2_128CBC = pbes2.RC2_128CBC

	RC5CBC     = pbes2.RC5CBC
	RC5_128CBC = pbes2.RC5_128CBC
	RC5_192CBC = pbes2.RC5_192CBC
	RC5_256CBC = pbes2.RC5_256CBC

	AES128ECB = pbes2.AES128ECB
	AES128CBC = pbes2.AES128CBC
	AES128OFB = pbes2.AES128OFB
	AES128CFB = pbes2.AES128CFB
	AES128GCM = pbes2.AES128GCM
	AES128CCM = pbes2.AES128CCM

	AES192ECB = pbes2.AES192ECB
	AES192CBC = pbes2.AES192CBC
	AES192OFB = pbes2.AES192OFB
	AES192CFB = pbes2.AES192CFB
	AES192GCM = pbes2.AES192GCM
	AES192CCM = pbes2.AES192CCM

	AES256ECB = pbes2.AES256ECB
	AES256CBC = pbes2.AES256CBC
	AES256OFB = pbes2.AES256OFB
	AES256CFB = pbes2.AES256CFB
	AES256GCM = pbes2.AES256GCM
	AES256CCM = pbes2.AES256CCM

	SM4Cipher = pbes2.SM4Cipher
	SM4ECB    = pbes2.SM4ECB
	SM4CBC    = pbes2.SM4CBC
	SM4OFB    = pbes2.SM4OFB
	SM4CFB    = pbes2.SM4CFB
	SM4CFB1   = pbes2.SM4CFB1
	SM4CFB8   = pbes2.SM4CFB8
	SM4GCM    = pbes2.SM4GCM
	SM4CCM    = pbes2.SM4CCM

	GostCipher = pbes2.GostCipher

	ARIA128ECB = pbes2.ARIA128ECB
	ARIA128CBC = pbes2.ARIA128CBC
	ARIA128CFB = pbes2.ARIA128CFB
	ARIA128OFB = pbes2.ARIA128OFB
	ARIA128CTR = pbes2.ARIA128CTR
	ARIA128GCM = pbes2.ARIA128GCM
	ARIA128CCM = pbes2.ARIA128CCM

	ARIA192ECB = pbes2.ARIA192ECB
	ARIA192CBC = pbes2.ARIA192CBC
	ARIA192CFB = pbes2.ARIA192CFB
	ARIA192OFB = pbes2.ARIA192OFB
	ARIA192CTR = pbes2.ARIA192CTR
	ARIA192GCM = pbes2.ARIA192GCM
	ARIA192CCM = pbes2.ARIA192CCM

	ARIA256ECB = pbes2.ARIA256ECB
	ARIA256CBC = pbes2.ARIA256CBC
	ARIA256CFB = pbes2.ARIA256CFB
	ARIA256OFB = pbes2.ARIA256OFB
	ARIA256CTR = pbes2.ARIA256CTR
	ARIA256GCM = pbes2.ARIA256GCM
	ARIA256CCM = pbes2.ARIA256CCM

	Misty1CBC = pbes2.Misty1CBC

	Serpent128ECB = pbes2.Serpent128ECB
	Serpent128CBC = pbes2.Serpent128CBC
	Serpent128OFB = pbes2.Serpent128OFB
	Serpent128CFB = pbes2.Serpent128CFB

	Serpent192ECB = pbes2.Serpent192ECB
	Serpent192CBC = pbes2.Serpent192CBC
	Serpent192OFB = pbes2.Serpent192OFB
	Serpent192CFB = pbes2.Serpent192CFB

	Serpent256ECB = pbes2.Serpent256ECB
	Serpent256CBC = pbes2.Serpent256CBC
	Serpent256OFB = pbes2.Serpent256OFB
	Serpent256CFB = pbes2.Serpent256CFB

	// seed
	SeedECB = pbes2.SeedECB
	SeedCBC = pbes2.SeedCBC
	SeedOFB = pbes2.SeedOFB
	SeedCFB = pbes2.SeedCFB

	Seed256ECB = pbes2.Seed256ECB
	Seed256CBC = pbes2.Seed256CBC
	Seed256OFB = pbes2.Seed256OFB
	Seed256CFB = pbes2.Seed256CFB
)

加密方式

View Source
var (
	// 默认 hash
	DefaultHash = SHA1
)
View Source
var DefaultOpts = Opts{
	Cipher:  AES256CBC,
	KDFOpts: DefaultPBKDF2Opts,
}

默认配置

View Source
var DefaultPBKDF2Opts = PBKDF2Opts{
	SaltSize:       16,
	IterationCount: 10000,
}

默认配置 PBKDF2

View Source
var (
	// 默认 hash
	DefaultSMHash = SM3
)
View Source
var DefaultSMOpts = Opts{
	Cipher:  SM4CBC,
	KDFOpts: DefaultSMPBKDF2Opts,
}

默认 GmSM 配置

View Source
var DefaultSMPBKDF2Opts = SMPBKDF2Opts{
	SaltSize:       16,
	IterationCount: 10000,
	HMACHash:       DefaultSMHash,
}

默认配置 GmSM PBKDF2

View Source
var DefaultScryptOpts = ScryptOpts{
	SaltSize:                 16,
	CostParameter:            1 << 2,
	BlockSize:                8,
	ParallelizationParameter: 1,
}

默认配置 Scrypt

View Source
var HashMap = map[string]Hash{
	"MD5":             MD5,
	"SHA1":            SHA1,
	"SHA224":          SHA224,
	"SHA256":          SHA256,
	"SHA384":          SHA384,
	"SHA512":          SHA512,
	"SHA512_224":      SHA512_224,
	"SHA512_256":      SHA512_256,
	"SM3":             SM3,
	"GOST34112012256": GOST34112012256,
	"GOST34112012512": GOST34112012512,
}

hash 列表

Functions

func AddKDF

func AddKDF(oid asn1.ObjectIdentifier, params func() KDFParameters)

添加 kdf 方式 add kdf type

func CheckPBES2 added in v1.0.3020

func CheckPBES2(oid asn1.ObjectIdentifier) bool

return true if has pbes2, else return false

func DecryptPEMBlock

func DecryptPEMBlock(block *pem.Block, password []byte) ([]byte, error)

解出 PEM 块

func DecryptPKCS8PrivateKey

func DecryptPKCS8PrivateKey(data, password []byte) ([]byte, error)

解密 PKCS8

func EncryptPKCS8PrivateKey

func EncryptPKCS8PrivateKey(
	rand io.Reader,
	blockType string,
	data []byte,
	password []byte,
	opts ...Opts,
) (*pem.Block, error)

加密 PKCS8

func IsPBES2 added in v1.0.2031

func IsPBES2(algo asn1.ObjectIdentifier) bool

是否是 PBES2 加密

func IsSMPBES2 added in v1.0.3019

func IsSMPBES2(algo asn1.ObjectIdentifier) bool

是否是 GmSM PBES2 加密

func PBES2Decrypt added in v1.0.2031

func PBES2Decrypt(data []byte, algo pkix.AlgorithmIdentifier, password []byte) ([]byte, error)

PBES2 解密

func PBES2Encrypt added in v1.0.2031

func PBES2Encrypt(rand io.Reader, data []byte, password []byte, opt *Opts) (encrypted []byte, algo pkix.AlgorithmIdentifier, err error)

PBES2 加密

Types

type Cipher

type Cipher = pbes2.Cipher

别名

type Hash

type Hash uint

pkcs8 可使用的 hash 方式

const (
	MD5 Hash = 1 + iota
	SHA1
	SHA224
	SHA256
	SHA384
	SHA512
	SHA512_224
	SHA512_256
	SM3
	GOST34112012256
	GOST34112012512
)

func GetHashFromName

func GetHashFromName(name string) Hash

获取 hash 类型

type KDFOpts

type KDFOpts interface {
	// 随机数大小
	GetSaltSize() int

	// oid
	OID() asn1.ObjectIdentifier

	// PBES oid
	PBESOID() asn1.ObjectIdentifier

	// 设置是否有 KeyLength
	WithHasKeyLength(hasKeyLength bool) KDFOpts

	// 生成密钥
	DeriveKey(password, salt []byte, size int) (key []byte, params KDFParameters, err error)
}

KDF 设置接口

type KDFParameters

type KDFParameters interface {
	// PBES oid
	PBESOID() asn1.ObjectIdentifier

	// 生成密钥
	DeriveKey(password []byte, size int) (key []byte, err error)
}

数据接口

type Opts

type Opts struct {
	Cipher  Cipher
	KDFOpts KDFOpts
}

配置

func ParseOpts

func ParseOpts(opts ...any) (Opts, error)

解析配置

type PBKDF2Opts

type PBKDF2Opts struct {
	SaltSize       int
	IterationCount int
	HMACHash       Hash
	// contains filtered or unexported fields
}

PBKDF2 配置

func (PBKDF2Opts) DeriveKey

func (this PBKDF2Opts) DeriveKey(password, salt []byte, size int) (key []byte, params KDFParameters, err error)

func (PBKDF2Opts) GetSaltSize

func (this PBKDF2Opts) GetSaltSize() int

func (PBKDF2Opts) OID

func (this PBKDF2Opts) OID() asn1.ObjectIdentifier

func (PBKDF2Opts) PBESOID added in v1.0.3019

func (this PBKDF2Opts) PBESOID() asn1.ObjectIdentifier

func (PBKDF2Opts) WithHasKeyLength added in v1.0.2039

func (this PBKDF2Opts) WithHasKeyLength(hasKeyLength bool) KDFOpts

type SMPBKDF2Opts added in v1.0.3019

type SMPBKDF2Opts struct {
	SaltSize       int
	IterationCount int
	HMACHash       Hash
	// contains filtered or unexported fields
}

GmSM PBKDF2 配置

func (SMPBKDF2Opts) DeriveKey added in v1.0.3019

func (this SMPBKDF2Opts) DeriveKey(password, salt []byte, size int) (key []byte, params KDFParameters, err error)

func (SMPBKDF2Opts) GetSaltSize added in v1.0.3019

func (this SMPBKDF2Opts) GetSaltSize() int

func (SMPBKDF2Opts) OID added in v1.0.3019

func (this SMPBKDF2Opts) OID() asn1.ObjectIdentifier

func (SMPBKDF2Opts) PBESOID added in v1.0.3019

func (this SMPBKDF2Opts) PBESOID() asn1.ObjectIdentifier

func (SMPBKDF2Opts) WithHasKeyLength added in v1.0.3019

func (this SMPBKDF2Opts) WithHasKeyLength(hasKeyLength bool) KDFOpts

type ScryptOpts

type ScryptOpts struct {
	SaltSize                 int
	CostParameter            int
	BlockSize                int
	ParallelizationParameter int
	// contains filtered or unexported fields
}

ScryptOpts 设置

func (ScryptOpts) DeriveKey

func (this ScryptOpts) DeriveKey(password, salt []byte, size int) (key []byte, params KDFParameters, err error)

func (ScryptOpts) GetSaltSize

func (this ScryptOpts) GetSaltSize() int

func (ScryptOpts) OID

func (this ScryptOpts) OID() asn1.ObjectIdentifier

func (ScryptOpts) PBESOID added in v1.0.3019

func (this ScryptOpts) PBESOID() asn1.ObjectIdentifier

func (ScryptOpts) WithHasKeyLength added in v1.0.2039

func (this ScryptOpts) WithHasKeyLength(hasKeyLength bool) KDFOpts

Jump to

Keyboard shortcuts

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