inter

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2021 License: Apache-2.0 Imports: 6 Imported by: 2

README

crypto-standard

Standard crypto algorithm implement.

Table of Contents

usage

hash
    hasher := NewHasher(SHA3_512)
	hash, _ := hasher.Hash([]byte(msg))
	hashHex := hex.EncodeToString(hash)
symmetric encryption
    aes := new(AES)
    key := []byte("12345678123456781234567812345678")
    c, _ := aes.Encrypt(key, []byte(msg))
    o, _ := aes.Decrypt(key, c)
signature
    h, _ := hash.NewHasher(hash.KECCAK_256).Hash(msg)
    priv, _ := GenerateKey(AlgoP256K1)
    bytes, _ := priv.Bytes()
    //r, s is encoded, then about 72bytes
    sign, _ := priv.Sign(bytes, h)
    pub, _ := priv.PublicKey()
    b, err := pub.Verify(nil, sign, h)

The vast majority of flato's ecdsa check (non-guomi) actually uses the recovery method, which is called the 'recovery mode' of the check in this package. This method is more efficient. The main feature of using this kind of check in flato is that the public key is not provided during the verification, but the public key value is calculated by sign, then the address is calculated by the public key value, and the address is compared to complete the check.

Note: The use of this check in flato cannot use the above check method. If this is the case, please refer to the code below.

    //Calculation signature
    h, _ := hash.NewHasher(hash.KECCAK_256).Hash(msg)
    //The effect of incoming AlgoP256K1 or AlgoP256K1Recover is the same here
    priv, _ := GenerateKey(AlgoP256K1)  
    bytes, _ := priv.Bytes()
    //r, s is encoded, then about 72bytes
    sign, _ := priv.Sign(bytes, h) 

    //You may get an address from some way, but you can also calculate the address in this way here.
    pub, _ := priv.PublicKey()
    pubByte,_ := pub.Bytes()
    temp,_ := hash.NewHasher(hash.KECCAK_256).Hash(pubByte)
    address:= temp[12:]

    //Here's how you can use the address, hash, and signature data to complete the check.
    recoverPub := NewECDSAPublicKey().FromBytes(address, AlgoP256K1Recover)
    b, err := recoverPub.Verify(nil, sign, h)

api

hash

Instantiate Hasher func NewHasher(hashType HashType) *Hasher

Computational hash func (h *Hasher) Hash(msg []byte) (hash []byte, err error)

symmetric encryption

Encrypt func (ea *AES) Encrypt(key, originMsg []byte, reader io.Reader) (encryptedMsg []byte, err error)

Decrypt func (ea *AES) Decrypt(key, encryptedMsg []byte) (originMsg []byte, err error)

signature

Generate key pair func GenerateKey(opt int) (*ECDSAPrivateKey, error)

Generate signature func (key *ECDSAPrivateKey) Sign(k, digest []byte, reader io.Reader) (signature []byte, err error)

Verification signature func (key *ECDSAPublicKey) Verify(_ []byte, signature, digest []byte) (valid bool, err error)

Mockgen

Install mockgen : go get github.com/golang/mock/mockgen

How to use?

  • source: Specify interface file
  • destination: Generated file name
  • package:The package name of the generated file
  • imports: Dependent package that requires import
  • aux_files: Attach a file when there is more than one file in the interface file
  • build_flags: Parameters passed to the build tool

Eg.mockgen -destination mock/mock_crypto.go -package crypto -source crypto.go

GitCZ

Note: Please use command npm install if you are the first time to use git cz in this repo.

Contribute

PRs are welcome!

Small note: If editing the Readme, please conform to the standard-readme specification.

License

crypto-standard is currently under Apache 2.0 license. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PKCS5Padding

func PKCS5Padding(ciphertext []byte, blockSize int) []byte

PKCS5Padding padding with pkcs5

func PKCS5UnPadding

func PKCS5UnPadding(origData []byte) ([]byte, error)

PKCS5UnPadding unpadding with pkcs5

func TripleDesDec

func TripleDesDec(key, src []byte) ([]byte, error)

TripleDesDec decryption algorithm implements

func TripleDesDecrypt8

func TripleDesDecrypt8(crypted, key []byte) ([]byte, error)

TripleDesDecrypt8 3DES with 8 bytes key

func TripleDesEnc

func TripleDesEnc(key, src []byte, reader io.Reader) ([]byte, error)

TripleDesEnc encryption algorithm implements

func TripleDesEncrypt8

func TripleDesEncrypt8(origData, key []byte) ([]byte, error)

TripleDesEncrypt8 3DES with 8 bytes key

Types

type AES

type AES struct {
}

AES a AES instance is a tool to encrypt and decrypt

func (*AES) Decrypt

func (ea *AES) Decrypt(key, encryptedMsg []byte) (originMsg []byte, err error)

Decrypt decrypt

func (*AES) Encrypt

func (ea *AES) Encrypt(key, originMsg []byte, reader io.Reader) (encryptedMsg []byte, err error)

Encrypt encrypt

type AESKey

type AESKey []byte

AESKey represent aes key

func (AESKey) Bytes

func (a AESKey) Bytes() ([]byte, error)

Bytes return bytes

func (AESKey) FromBytes

func (a AESKey) FromBytes(k []byte, opt interface{}) []byte

FromBytes get a key from bytes

type TripleDES

type TripleDES struct {
}

TripleDES a 3DES instance is a tool to encrypt and decrypt Very not recommended to use 3des!!! It's slow and unsafe

func (*TripleDES) Decrypt

func (ea *TripleDES) Decrypt(key, cipherTex []byte) (plaintext []byte, err error)

Decrypt decrypt

func (*TripleDES) Encrypt

func (ea *TripleDES) Encrypt(key, plaintext []byte, reader io.Reader) (cipherText []byte, err error)

Encrypt encrypt

type TripleDESKey

type TripleDESKey []byte

TripleDESKey represent 3des key

func (TripleDESKey) Bytes

func (t TripleDESKey) Bytes() ([]byte, error)

Bytes return bytes

func (TripleDESKey) FromBytes

func (t TripleDESKey) FromBytes(k []byte, opt interface{}) []byte

FromBytes get a key from bytes

Directories

Path Synopsis
sha3
Hyperchain License Copyright (C) 2016 The Hyperchain Authors.
Hyperchain License Copyright (C) 2016 The Hyperchain Authors.

Jump to

Keyboard shortcuts

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