crypto

package
v0.0.0-...-2325473 Latest Latest
Warning

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

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

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. * ==============================================================================

Index

Constants

View Source
const (
	RSA = "RSA" //-- "RSA/ECB/PKCS1Padding", "SHA256withRSA"
	ECC = "ECC"
)
View Source
const (
	AES = "AES" //-- "AES/CBC/PKCS7Padding"
	DES = "DES"
)

Variables

This section is empty.

Functions

func AsymmetricKeysMatch

func AsymmetricKeysMatch(sKey SignKey, pKey VerifyKey) bool

*

  • Check asymmetric keys *
  • @param sKey - private key
  • @param pKey - public key
  • @return true on keys matched

func CryptographyKeyGetAlgorithm

func CryptographyKeyGetAlgorithm(key map[string]interface{}) string

func PrivateKeySetFactory

func PrivateKeySetFactory(algorithm string, factory PrivateKeyFactory)

func PublicKeySetFactory

func PublicKeySetFactory(algorithm string, factory PublicKeyFactory)

func SymmetricKeySetFactory

func SymmetricKeySetFactory(algorithm string, factory SymmetricKeyFactory)

func SymmetricKeysMatch

func SymmetricKeysMatch(pKey EncryptKey, sKey DecryptKey) bool

*

  • Check symmetric keys *
  • @param pKey - symmetric key1
  • @param sKey - symmetric key2
  • @return true on keys equal

Types

type AsymmetricKey

type AsymmetricKey interface {
	CryptographyKey
}

*

  • Asymmetric Cryptography Key
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~

type CryptographyKey

type CryptographyKey interface {
	Mapper

	/**
	 *  Get key algorithm name
	 *
	 * @return algorithm name
	 */
	Algorithm() string

	/**
	 *  Get key data
	 *
	 * @return key data
	 */
	Data() []byte
}

*

  • Cryptography Key
  • ~~~~~~~~~~~~~~~~
  • Cryptography key with designated algorithm *
  • key data format: {
  • algorithm : "RSA", // ECC, AES, ...
  • data : "{BASE64_ENCODE}",
  • ...
  • }

type DecryptKey

type DecryptKey interface {
	IDecryptKey
	CryptographyKey
}

type EncryptKey

type EncryptKey interface {
	IEncryptKey
	CryptographyKey
}

type IDecryptKey

type IDecryptKey interface {

	/**
	 *  plaintext = decrypt(ciphertext, PW);
	 *  plaintext = decrypt(ciphertext, SK);
	 *
	 * @param ciphertext - encrypted data
	 * @return plaintext
	 */
	Decrypt(ciphertext []byte) []byte

	/**
	 *  OK = decrypt(encrypt(data, SK), PK) == data
	 *
	 * @param pKey - public key
	 * @return true on signature matched
	 */
	Match(pKey EncryptKey) bool
}

type IEncryptKey

type IEncryptKey interface {

	/**
	 *  ciphertext = encrypt(plaintext, PW)
	 *  ciphertext = encrypt(plaintext, PK)
	 *
	 * @param plaintext - plain data
	 * @return ciphertext
	 */
	Encrypt(plaintext []byte) []byte
}

type ISignKey

type ISignKey interface {

	/**
	 *  signature = sign(data, SK);
	 *
	 * @param data - data to be signed
	 * @return signature
	 */
	Sign(data []byte) []byte
}

type IVerifyKey

type IVerifyKey interface {

	/**
	 *  OK = verify(data, signature, PK)
	 *
	 * @param data - data
	 * @param signature - signature of data
	 * @return true on signature matched
	 */
	Verify(data []byte, signature []byte) bool

	/**
	 *  OK = verify(data, sign(data, SK), PK)
	 *
	 * @param sKey - private key
	 * @return true on signature matched
	 */
	Match(sKey SignKey) bool
}

type PrivateKey

type PrivateKey interface {
	AsymmetricKey
	ISignKey

	/**
	 *  Get public key from private key
	 *
	 * @return public key paired to this private key
	 */
	PublicKey() PublicKey
}

*

  • Asymmetric Cryptography Private Key
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • This class is used to decrypt symmetric key or sign message data *
  • key data format: {
  • algorithm : "RSA", // "ECC", ...
  • data : "{BASE64_ENCODE}",
  • ...
  • }

func PrivateKeyGenerate

func PrivateKeyGenerate(algorithm string) PrivateKey

Factory methods

func PrivateKeyParse

func PrivateKeyParse(key interface{}) PrivateKey

type PrivateKeyFactory

type PrivateKeyFactory interface {

	/**
	 *  Generate key
	 *
	 * @return PrivateKey
	 */
	GeneratePrivateKey() PrivateKey

	/**
	 *  Parse map object to key
	 *
	 * @param key - key info
	 * @return PrivateKey
	 */
	ParsePrivateKey(key map[string]interface{}) PrivateKey
}

*

  • Private Key Factory
  • ~~~~~~~~~~~~~~~~~~~

func PrivateKeyGetFactory

func PrivateKeyGetFactory(algorithm string) PrivateKeyFactory

type PublicKey

type PublicKey interface {
	AsymmetricKey
	IVerifyKey
}

*

  • Asymmetric Cryptography Public Key
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
  • key data format: {
  • algorithm : "RSA", // "ECC", ...
  • data : "{BASE64_ENCODE}",
  • ...
  • }

func PublicKeyParse

func PublicKeyParse(key interface{}) PublicKey

Factory method

type PublicKeyFactory

type PublicKeyFactory interface {

	/**
	 *  Parse map object to key
	 *
	 * @param key - key info
	 * @return PublicKey
	 */
	ParsePublicKey(key map[string]interface{}) PublicKey
}

*

  • Public Key Factory
  • ~~~~~~~~~~~~~~~~~~

func PublicKeyGetFactory

func PublicKeyGetFactory(algorithm string) PublicKeyFactory

type SignKey

type SignKey interface {
	ISignKey
	CryptographyKey
}

type SymmetricKey

type SymmetricKey interface {
	CryptographyKey
	IEncryptKey
	IDecryptKey
}

*

  • Symmetric Cryptography Key
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~
  • This class is used to encrypt or decrypt message data *
  • key data format: {
  • algorithm : "AES", // "DES", ...
  • data : "{BASE64_ENCODE}",
  • ...
  • }

func SymmetricKeyGenerate

func SymmetricKeyGenerate(algorithm string) SymmetricKey

Factory methods

func SymmetricKeyParse

func SymmetricKeyParse(key interface{}) SymmetricKey

type SymmetricKeyFactory

type SymmetricKeyFactory interface {

	/**
	 *  Generate key
	 *
	 * @return SymmetricKey
	 */
	GenerateSymmetricKey() SymmetricKey

	/**
	 *  Parse map object to key
	 *
	 * @param key - key info
	 * @return SymmetricKey
	 */
	ParseSymmetricKey(key map[string]interface{}) SymmetricKey
}

*

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

func SymmetricKeyGetFactory

func SymmetricKeyGetFactory(algorithm string) SymmetricKeyFactory

type VerifyKey

type VerifyKey interface {
	IVerifyKey
	CryptographyKey
}

Jump to

Keyboard shortcuts

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