crypto

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 18 Imported by: 0

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 * * DIMP : Decentralized Instant Messaging Protocol * * Written in 2026 by Moky <albert.moky@gmail.com> * * ============================================================================== * The MIT License (MIT) * * Copyright (c) 2026 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) 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) 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

This section is empty.

Functions

func GetKeyAlgorithm

func GetKeyAlgorithm(key StringKeyMap) string

func MatchEncryptKey

func MatchEncryptKey(pKey EncryptKey, sKey DecryptKey) bool

func MatchSignKey

func MatchSignKey(sKey SignKey, pKey VerifyKey) bool

func NewAESKey

func NewAESKey() SymmetricKey

generate key

func NewAESKeyWithMap

func NewAESKeyWithMap(dict StringKeyMap) SymmetricKey

func NewECCPrivateKey

func NewECCPrivateKey() PrivateKey

generate key

func NewECCPrivateKeyWithMap

func NewECCPrivateKeyWithMap(dict StringKeyMap) PrivateKey

func NewECCPublicKeyWithMap

func NewECCPublicKeyWithMap(dict StringKeyMap) PublicKey

func NewPlainKey

func NewPlainKey() SymmetricKey

func NewPlainKeyWithMap

func NewPlainKeyWithMap(dict StringKeyMap) SymmetricKey

Types

type AESKey

type AESKey struct {
	//SymmetricKey
	*Dictionary
	// contains filtered or unexported fields
}

AESKey implements the SymmetricKey interface for AES encryption/decryption

Standard symmetric key for secure message encryption with configurable key sizes (typically 32 bytes for AES-256)

KeyInfo JSON Format: {
    "algorithm" : "AES",
    "keySize"   : 32,         // Optional: Key size in bytes (16=AES-128, 24=AES-192, 32=AES-256)
    "data"      : "{BASE64}"  // Base64-encoded raw key material
}

func (*AESKey) Algorithm

func (key *AESKey) Algorithm() string

Override

func (*AESKey) Data

func (key *AESKey) Data() TransportableData

Override

func (*AESKey) Decrypt

func (key *AESKey) Decrypt(ciphertext []byte, params StringKeyMap) []byte

Override

func (*AESKey) Encrypt

func (key *AESKey) Encrypt(plaintext []byte, extra StringKeyMap) []byte

Override

func (*AESKey) Equal

func (key *AESKey) Equal(other any) bool

Override

func (*AESKey) MatchEncryptKey

func (key *AESKey) MatchEncryptKey(pKey EncryptKey) bool

Override

type ECCPrivateKey

type ECCPrivateKey struct {
	//PrivateKey
	*Dictionary
	// contains filtered or unexported fields
}

ECCPrivateKey implements the PrivateKey interface for ECC (Elliptic Curve Cryptography)

Uses secp256k1 curve (Bitcoin/Ethereum standard) for asymmetric cryptography

KeyInfo JSON Format: {
    "algorithm" : "ECC",
    "curve"     : "secp256k1",  // Elliptic curve identifier (secp256k1 is primary supported curve)
    "data"      : "{BASE64}"    // Base64-encoded raw ECC private key material
}

func (*ECCPrivateKey) Algorithm

func (key *ECCPrivateKey) Algorithm() string

Override

func (*ECCPrivateKey) Data

func (key *ECCPrivateKey) Data() TransportableData

Override

func (*ECCPrivateKey) Equal

func (key *ECCPrivateKey) Equal(other any) bool

Override

func (*ECCPrivateKey) PublicKey

func (key *ECCPrivateKey) PublicKey() PublicKey

Override

func (*ECCPrivateKey) Sign

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

Override

type ECCPublicKey

type ECCPublicKey struct {
	//PublicKey
	*Dictionary
	// contains filtered or unexported fields
}

ECCPublicKey implements the PublicKey interface for ECC (Elliptic Curve Cryptography)

Corresponding public key for ECCPrivateKey, uses secp256k1 curve

KeyInfo JSON Format: {
    "algorithm": "ECC",
    "curve": "secp256k1",  // Elliptic curve identifier (matches private key curve)
    "data": "{BASE64}"     // Base64-encoded raw ECC public key material
}

func (*ECCPublicKey) Algorithm

func (key *ECCPublicKey) Algorithm() string

Override

func (*ECCPublicKey) Data

func (key *ECCPublicKey) Data() TransportableData

Override

func (*ECCPublicKey) MatchSignKey

func (key *ECCPublicKey) MatchSignKey(sKey SignKey) bool

Override

func (*ECCPublicKey) Verify

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

Override

type IRSAPrivateKey

type IRSAPrivateKey interface {
	PrivateKey
	DecryptKey
}

func NewRSAPrivateKey

func NewRSAPrivateKey() IRSAPrivateKey

generate key

func NewRSAPrivateKeyWithMap

func NewRSAPrivateKeyWithMap(dict StringKeyMap) IRSAPrivateKey

type IRSAPublicKey

type IRSAPublicKey interface {
	PublicKey
	EncryptKey
}

func NewRSAPublicKeyWithMap

func NewRSAPublicKeyWithMap(dict StringKeyMap) IRSAPublicKey

type PlainKey

type PlainKey struct {
	//SymmetricKey
	*Dictionary
	// contains filtered or unexported fields
}

PlainKey implements the SymmetricKey interface for unencrypted/broadcast messages

"Null" symmetric key that performs no actual encryption/decryption on message data

Designed for broadcast messages where content should remain in plaintext

func (*PlainKey) Algorithm

func (key *PlainKey) Algorithm() string

Override

func (*PlainKey) Data

func (key *PlainKey) Data() TransportableData

Override

func (*PlainKey) Decrypt

func (key *PlainKey) Decrypt(ciphertext []byte, _ StringKeyMap) []byte

Override

func (*PlainKey) Encrypt

func (key *PlainKey) Encrypt(plaintext []byte, _ StringKeyMap) []byte

Override

func (*PlainKey) Equal

func (key *PlainKey) Equal(other any) bool

Override

func (*PlainKey) MatchEncryptKey

func (key *PlainKey) MatchEncryptKey(pKey EncryptKey) bool

Override

type RSAPrivateKey

type RSAPrivateKey struct {
	//IRSAPrivateKey
	*Dictionary
	// contains filtered or unexported fields
}

RSAPrivateKey implements the IRSAPrivateKey interface for RSA asymmetric cryptography

Provides RSA private key capabilities (signing, decryption, key pair management)

KeyInfo JSON Format: {
    "algorithm": "RSA",
    "data": "{BASE64}"   // Base64-encoded raw RSA private key material (PKCS#8 format)
}

func (*RSAPrivateKey) Algorithm

func (key *RSAPrivateKey) Algorithm() string

Override

func (*RSAPrivateKey) Data

func (key *RSAPrivateKey) Data() TransportableData

Override

func (*RSAPrivateKey) Decrypt

func (key *RSAPrivateKey) Decrypt(ciphertext []byte, _ StringKeyMap) []byte

Override

func (*RSAPrivateKey) Equal

func (key *RSAPrivateKey) Equal(other any) bool

Override

func (*RSAPrivateKey) MatchEncryptKey

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

Override

func (*RSAPrivateKey) PublicKey

func (key *RSAPrivateKey) PublicKey() PublicKey

Override

func (*RSAPrivateKey) Sign

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

Override

type RSAPublicKey

type RSAPublicKey struct {
	//PublicKey, EncryptKey
	*Dictionary
	// contains filtered or unexported fields
}

RSAPublicKey implements the PublicKey and EncryptKey interfaces for RSA cryptography

Corresponding public key for RSAPrivateKey (encryption, signature verification)

KeyInfo JSON Format: {
    "algorithm": "RSA",
    "data": "{BASE64}"   // Base64-encoded raw RSA public key material (PKCS#1 format)
}

func (*RSAPublicKey) Algorithm

func (key *RSAPublicKey) Algorithm() string

Override

func (*RSAPublicKey) Data

func (key *RSAPublicKey) Data() TransportableData

Override

func (*RSAPublicKey) Encrypt

func (key *RSAPublicKey) Encrypt(plaintext []byte, _ StringKeyMap) []byte

Override

func (*RSAPublicKey) MatchSignKey

func (key *RSAPublicKey) MatchSignKey(sKey SignKey) bool

Override

func (*RSAPublicKey) Verify

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

Override

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