crypto

package
v0.0.0-...-8128143 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2022 License: MIT Imports: 15 Imported by: 2

Documentation

Overview

license: https://mit-license.org * ============================================================================== * The MIT License (MIT) * * Copyright (c) 2020 Albert Moky * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * ==============================================================================

license: https://mit-license.org * ============================================================================== * The MIT License (MIT) * * Copyright (c) 2021 Albert Moky * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * ==============================================================================

license: https://mit-license.org * ============================================================================== * The MIT License (MIT) * * Copyright (c) 2021 Albert Moky * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * ==============================================================================

license: https://mit-license.org * ============================================================================== * The MIT License (MIT) * * Copyright (c) 2020 Albert Moky * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * ==============================================================================

license: https://mit-license.org * ============================================================================== * The MIT License (MIT) * * Copyright (c) 2021 Albert Moky * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * ==============================================================================

license: https://mit-license.org * ============================================================================== * The MIT License (MIT) * * Copyright (c) 2020 Albert Moky * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * ==============================================================================

Index

Constants

This section is empty.

Variables

View Source
var PLAIN = "PLAIN"

*

  • Symmetric key for broadcast message,
  • which will do nothing when en/decoding message data

Functions

func GetPlainKey

func GetPlainKey() SymmetricKey

func MarshalPKCS8PrivateKey

func MarshalPKCS8PrivateKey(key *rsa.PrivateKey) []byte

func NewAESKey

func NewAESKey(dict map[string]interface{}) SymmetricKey

func NewECCPrivateKey

func NewECCPrivateKey(dict map[string]interface{}) PrivateKey

func NewECCPublicKey

func NewECCPublicKey(dict map[string]interface{}) PublicKey

func NewPlainKey

func NewPlainKey() SymmetricKey

func NewPrivateKeyFactory

func NewPrivateKeyFactory(g PrivateKeyGenerator, p PrivateKeyParser) PrivateKeyFactory

func NewPublicKeyFactory

func NewPublicKeyFactory(p PublicKeyParser) PublicKeyFactory

func NewRSAPrivateKey

func NewRSAPrivateKey(dict map[string]interface{}) PrivateKey

func NewRSAPublicKey

func NewRSAPublicKey(dict map[string]interface{}) PublicKey

func NewSymmetricKeyFactory

func NewSymmetricKeyFactory(g SymmetricKeyGenerator, p SymmetricKeyParser) SymmetricKeyFactory

func PKCS5Padding

func PKCS5Padding(src []byte, blockSize uint) []byte

func PKCS5UnPadding

func PKCS5UnPadding(src []byte) []byte

func RegisterPrivateKeyFactories

func RegisterPrivateKeyFactories()

func RegisterPublicKeyFactories

func RegisterPublicKeyFactories()

func RegisterSymmetricKeyFactories

func RegisterSymmetricKeyFactories()

Types

type AESKey

type AESKey struct {
	BaseSymmetricKey
	// contains filtered or unexported fields
}

*

  • AES Key *
  • keyInfo format: {
  • algorithm: "AES",
  • keySize : 32, // optional
  • data : "{BASE64_ENCODE}}" // password data
  • iv : "{BASE64_ENCODE}", // initialization vector
  • }

func (*AESKey) Data

func (key *AESKey) Data() []byte

func (*AESKey) Decrypt

func (key *AESKey) Decrypt(ciphertext []byte) []byte

func (*AESKey) Encrypt

func (key *AESKey) Encrypt(plaintext []byte) []byte

func (*AESKey) Init

func (key *AESKey) Init(dict map[string]interface{}) SymmetricKey

type BaseKey

type BaseKey struct {
	Dictionary
}

func (*BaseKey) Algorithm

func (key *BaseKey) Algorithm() string

func (*BaseKey) Data

func (key *BaseKey) Data() []byte

type BasePrivateKey

type BasePrivateKey struct {
	BaseKey
}

*

  • Asymmetric Private Key
  • ~~~~~~~~~~~~~~~~~~~~~~

type BasePublicKey

type BasePublicKey struct {
	BaseKey
}

*

  • Asymmetric Public Key
  • ~~~~~~~~~~~~~~~~~~~~~

func (*BasePublicKey) Match

func (key *BasePublicKey) Match(sKey SignKey) bool

func (*BasePublicKey) Verify

func (key *BasePublicKey) Verify(_ []byte, _ []byte) bool

type BaseSymmetricKey

type BaseSymmetricKey struct {
	BaseKey
}

*

  • Symmetric Key
  • ~~~~~~~~~~~~~

func (*BaseSymmetricKey) Decrypt

func (key *BaseSymmetricKey) Decrypt(_ []byte) []byte

func (*BaseSymmetricKey) Match

func (key *BaseSymmetricKey) Match(pKey EncryptKey) bool

type ECCPrivateKey

type ECCPrivateKey struct {
	BasePrivateKey
	// contains filtered or unexported fields
}

*

  • ECC Private Key *
  • keyInfo format: {
  • algorithm : "ECC",
  • data : "..." // base64_encode()
  • }

func (*ECCPrivateKey) Data

func (key *ECCPrivateKey) Data() []byte

func (*ECCPrivateKey) Init

func (key *ECCPrivateKey) Init(dict map[string]interface{}) PrivateKey

func (*ECCPrivateKey) PublicKey

func (key *ECCPrivateKey) PublicKey() PublicKey

func (*ECCPrivateKey) Sign

func (key *ECCPrivateKey) Sign(data []byte) []byte

type ECCPublicKey

type ECCPublicKey struct {
	BasePublicKey
	// contains filtered or unexported fields
}

*

  • ECC Public Key *
  • keyInfo format: {
  • algorithm : "ECC",
  • data : "..." // base64_encode()
  • }

func (*ECCPublicKey) Data

func (key *ECCPublicKey) Data() []byte

func (*ECCPublicKey) Init

func (key *ECCPublicKey) Init(dict map[string]interface{}) PublicKey

func (*ECCPublicKey) Verify

func (key *ECCPublicKey) Verify(data []byte, signature []byte) bool

type GeneralPrivateKeyFactory

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

private key factory

func (*GeneralPrivateKeyFactory) GeneratePrivateKey

func (factory *GeneralPrivateKeyFactory) GeneratePrivateKey() PrivateKey

func (*GeneralPrivateKeyFactory) Init

func (factory *GeneralPrivateKeyFactory) Init(g PrivateKeyGenerator, p PrivateKeyParser) PrivateKeyFactory

func (*GeneralPrivateKeyFactory) ParsePrivateKey

func (factory *GeneralPrivateKeyFactory) ParsePrivateKey(key map[string]interface{}) PrivateKey

type GeneralPublicKeyFactory

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

public key factory

func (*GeneralPublicKeyFactory) Init

func (factory *GeneralPublicKeyFactory) Init(p PublicKeyParser) PublicKeyFactory

func (*GeneralPublicKeyFactory) ParsePublicKey

func (factory *GeneralPublicKeyFactory) ParsePublicKey(key map[string]interface{}) PublicKey

type GeneralSymmetricKeyFactory

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

symmetric key factory

func (*GeneralSymmetricKeyFactory) GenerateSymmetricKey

func (factory *GeneralSymmetricKeyFactory) GenerateSymmetricKey() SymmetricKey

func (*GeneralSymmetricKeyFactory) Init

func (factory *GeneralSymmetricKeyFactory) Init(g SymmetricKeyGenerator, p SymmetricKeyParser) SymmetricKeyFactory

func (*GeneralSymmetricKeyFactory) ParseSymmetricKey

func (factory *GeneralSymmetricKeyFactory) ParseSymmetricKey(key map[string]interface{}) SymmetricKey

type PlainKey

type PlainKey struct {
	BaseSymmetricKey
	// contains filtered or unexported fields
}

func (*PlainKey) Data

func (key *PlainKey) Data() []byte

func (*PlainKey) Decrypt

func (key *PlainKey) Decrypt(ciphertext []byte) []byte

func (*PlainKey) Encrypt

func (key *PlainKey) Encrypt(plaintext []byte) []byte

func (*PlainKey) Init

func (key *PlainKey) Init(dict map[string]interface{}) SymmetricKey

type PrivateKeyGenerator

type PrivateKeyGenerator func() PrivateKey

*

  • Asymmetric Key

type PrivateKeyParser

type PrivateKeyParser func(map[string]interface{}) PrivateKey

type PublicKeyParser

type PublicKeyParser func(map[string]interface{}) PublicKey

type RSAPrivateKey

type RSAPrivateKey struct {
	BasePrivateKey
	// contains filtered or unexported fields
}

*

  • RSA Private Key *
  • keyInfo format: {
  • algorithm : "RSA",
  • data : "..." // base64_encode()
  • }

func (*RSAPrivateKey) Data

func (key *RSAPrivateKey) Data() []byte

func (*RSAPrivateKey) Decrypt

func (key *RSAPrivateKey) Decrypt(ciphertext []byte) []byte

func (*RSAPrivateKey) Init

func (key *RSAPrivateKey) Init(dict map[string]interface{}) PrivateKey

func (*RSAPrivateKey) Match

func (key *RSAPrivateKey) Match(pKey EncryptKey) bool

func (*RSAPrivateKey) PublicKey

func (key *RSAPrivateKey) PublicKey() PublicKey

func (*RSAPrivateKey) Sign

func (key *RSAPrivateKey) Sign(data []byte) []byte

type RSAPublicKey

type RSAPublicKey struct {
	BasePublicKey
	// contains filtered or unexported fields
}

*

  • RSA Public Key *
  • keyInfo format: {
  • algorithm: "RSA",
  • data: "..." // base64
  • }

func (*RSAPublicKey) Data

func (key *RSAPublicKey) Data() []byte

func (*RSAPublicKey) Encrypt

func (key *RSAPublicKey) Encrypt(plaintext []byte) []byte

func (*RSAPublicKey) Init

func (key *RSAPublicKey) Init(dict map[string]interface{}) PublicKey

func (*RSAPublicKey) Verify

func (key *RSAPublicKey) Verify(data []byte, signature []byte) bool

type SymmetricKeyGenerator

type SymmetricKeyGenerator func() SymmetricKey

*

  • Symmetric Key

type SymmetricKeyParser

type SymmetricKeyParser func(map[string]interface{}) SymmetricKey

Directories

Path Synopsis
license: https://mit-license.org * ============================================================================== * The MIT License (MIT) * * Copyright (c) 2021 Albert Moky * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software.
license: https://mit-license.org * ============================================================================== * The MIT License (MIT) * * Copyright (c) 2021 Albert Moky * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software.

Jump to

Keyboard shortcuts

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