crypto

package module
v0.0.0-...-0e75632 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2021 License: MIT Imports: 3 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) 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 whether they are key pair *
  • @param sKey - private key
  • @param pKey - public key
  • @return true on keys matched

func CryptographyKeyGetAlgorithm

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

func CryptographyKeysMatch

func CryptographyKeysMatch(pKey EncryptKey, sKey DecryptKey) bool

func PrivateKeyRegister

func PrivateKeyRegister(algorithm string, factory PrivateKeyFactory)

func PublicKeyRegister

func PublicKeyRegister(algorithm string, factory PublicKeyFactory)

func SymmetricKeyRegister

func SymmetricKeyRegister(algorithm string, factory SymmetricKeyFactory)

Types

type AsymmetricKey

type AsymmetricKey interface {
	CryptographyKey
}

type CryptographyKey

type CryptographyKey interface {
	Map

	/**
	 *  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 {

	/**
	 *  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 EncryptKey

type EncryptKey interface {

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

type PrivateKey

type PrivateKey interface {
	AsymmetricKey
	SignKey

	/**
	 *  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
	VerifyKey
}

*

  • 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 {

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

type SymmetricKey

type SymmetricKey interface {
	CryptographyKey
	EncryptKey
	DecryptKey
}

*

  • 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 {

	/**
	 *  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
}

Directories

Path Synopsis
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.
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.
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