crypto

package
v0.0.0-...-c9d2d86 Latest Latest
Warning

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

Go to latest
Published: May 25, 2018 License: ISC Imports: 26 Imported by: 0

Documentation

Overview

Copyright (C) 2017, Zipper Team. All rights reserved.

This file is part of zipper

The zipper is free software: you can use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

The zipper is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ISC License for more details.

You should have received a copy of the ISC License along with this program. If not, see <https://opensource.org/licenses/isc>.

Copyright (C) 2017, Zipper Team. All rights reserved.

This file is part of zipper

The zipper is free software: you can use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

The zipper is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ISC License for more details.

You should have received a copy of the ISC License along with this program. If not, see <https://opensource.org/licenses/isc>.

Copyright (C) 2017, Zipper Team. All rights reserved.

This file is part of zipper

The zipper is free software: you can use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

The zipper is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ISC License for more details.

You should have received a copy of the ISC License along with this program. If not, see <https://opensource.org/licenses/isc>.

Copyright (C) 2017, Zipper Team. All rights reserved.

This file is part of zipper

The zipper is free software: you can use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

The zipper is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ISC License for more details.

You should have received a copy of the ISC License along with this program. If not, see <https://opensource.org/licenses/isc>.

Index

Constants

View Source
const (
	// HashSize represents the hash length
	HashSize = 32
)
View Source
const (
	// SignatureSize represents the signature length
	SignatureSize = 65
)

Variables

View Source
var (
	// N is secp256k1 N
	N = S256().Params().N
)

Functions

func AesCTRXOR

func AesCTRXOR(key, inText, iv []byte) ([]byte, error)

AesCTRXOR encrypts data with CTRXOR

func AesDecrypt

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

AesDecrypt decrypts data using the specified key

func AesEncrypt

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

AesEncrypt encrypts data using the specified key

func Ecrecover

func Ecrecover(hash, sig []byte) ([]byte, error)

Ecrecover recovers publick key

func GeneratePrivateKeyBytes

func GeneratePrivateKeyBytes(privateKey *rsa.PrivateKey) ([]byte, error)

func GenerateRootCertificateBytes

func GenerateRootCertificateBytes(rootCertificate *x509.Certificate, rootPrivateKey *rsa.PrivateKey) ([]byte, error)

func Keccak256

func Keccak256(data ...[]byte) []byte

Keccak256 calculates and returns the Keccak256 hash of the input data.

func LoadCrypter

func LoadCrypter(file string, name string) (crypter.IPrivateKey, error)

func NewCertificate

func NewCertificate(info CertInformation) *x509.Certificate

func PKCS5Padding

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

PKCS5Padding padding data

func PKCS5UnPadding

func PKCS5UnPadding(origData []byte) []byte

PKCS5UnPadding unpadding data

func Parse

func Parse(cert, key []byte) (rootCertificate *x509.Certificate, rootPrivateKey *rsa.PrivateKey, err error)

func ParseCrt

func ParseCrt(data []byte) (*x509.Certificate, error)

func ParseKey

func ParseKey(data []byte) (*rsa.PrivateKey, error)

func Ripemd160

func Ripemd160(data []byte) []byte

Ripemd160 calculates the returns ripemd160 hash of the input data

func S256

func S256() elliptic.Curve

S256 returns an instance of the secp256k1 curve

func SaveCrypter

func SaveCrypter(file string, name string, priv crypter.IPrivateKey) error

func SignRsa

func SignRsa(privateKey *rsa.PrivateKey, data []byte) ([]byte, error)

func VerifyCertificate

func VerifyCertificate(rootCertificate, Certificate *x509.Certificate) error

VerifyCertificate use root Certificate to verify remote Certificate

func VerifySign

func VerifySign(hashed [sha256.Size]byte, sign []byte, Certificate *x509.Certificate) error

func ZeroKey

func ZeroKey(k *PrivateKey)

ZeroKey clean private key

Types

type CertInformation

type CertInformation struct {
	Country            []string
	Organization       []string
	OrganizationalUnit []string
	EmailAddress       []string
	Province           []string
	Locality           []string
	CommonName         string
	IsCA               bool
	Names              []pkix.AttributeTypeAndValue
}

type Hash

type Hash [HashSize]byte

Hash represents the 32 byte hash of arbitrary data

func CalcHash

func CalcHash(a, b Hash) Hash

CalcHash calculates and returns the hash of the a + b

func ComputeMerkleHash

func ComputeMerkleHash(data []Hash) []Hash

ComputeMerkleHash returns the merkle root hash of the hash lists

func DoubleSha256

func DoubleSha256(data []byte) Hash

DoubleSha256 calculates and returns double sha256 hash of the input data

func GetMerkleHash

func GetMerkleHash(data []Hash) Hash

GetMerkleHash returns the final hash

func HexToHash

func HexToHash(s string) Hash

HexToHash coverts string `s` to hash

func NewHash

func NewHash(data []byte) Hash

NewHash constructs a hash use the giving bytes

func Sha256

func Sha256(data []byte) Hash

Sha256 calculates and returns sha256 hash of the input data

func (Hash) Bytes

func (h Hash) Bytes() []byte

Bytes returns the bytes respresentation of the hash

func (Hash) Equal

func (h Hash) Equal(h1 Hash) bool

Equal reports whether h1 equal h

func (Hash) MarshalText

func (h Hash) MarshalText() ([]byte, error)

MarshalText returns the hex representation of h.

func (Hash) PrefixLen

func (h Hash) PrefixLen() int

PrefixLen returns the length of the zero prefix

func (*Hash) Reverse

func (h *Hash) Reverse() Hash

Reverse sets self byte-reversed hash

func (*Hash) SetBytes

func (h *Hash) SetBytes(b []byte)

SetBytes sets the hash to the value of b

func (*Hash) SetString

func (h *Hash) SetString(s string)

SetString set string `s` to h

func (Hash) String

func (h Hash) String() string

String returns the string respresentation of the hash

func (*Hash) UnmarshalText

func (h *Hash) UnmarshalText(input []byte) error

UnmarshalText parses a hash in hex syntax.

func (Hash) Xor

func (h Hash) Xor(h1 Hash) Hash

Xor calculates the h ^ h1. returns resulting hash

type PrivateKey

type PrivateKey ecdsa.PrivateKey

PrivateKey represents the ecdsa privatekey

func GenerateKey

func GenerateKey() (*PrivateKey, error)

GenerateKey returns a random PrivateKey

func HexToECDSA

func HexToECDSA(hexkey string) (*PrivateKey, error)

HexToECDSA parses a secp256k1 private key

func LoadECDSA

func LoadECDSA(file string) (*PrivateKey, error)

LoadECDSA loads a private key from the given file

func ToECDSA

func ToECDSA(prv []byte) *PrivateKey

ToECDSA creates a private key with the given D value.

func (*PrivateKey) Public

func (priv *PrivateKey) Public() *PublicKey

Public returns the public key corresponding to priv.

func (*PrivateKey) SaveECDSA

func (priv *PrivateKey) SaveECDSA(file string) error

SaveECDSA saves a private key to the given file

func (*PrivateKey) SecretBytes

func (priv *PrivateKey) SecretBytes() []byte

SecretBytes returns the actual bytes of ecdsa privatekey

func (*PrivateKey) Sign

func (priv *PrivateKey) Sign(hash []byte) (sig *Signature, err error)

Sign signs the hash and returns the signature

type PublicKey

type PublicKey ecdsa.PublicKey

PublicKey represents the ecdsa publickey

func SigToPub

func SigToPub(hash, sig []byte) (*PublicKey, error)

SigToPub recovers public key from the input data to the ecdsa public key

func ToECDSAPub

func ToECDSAPub(pub []byte) *PublicKey

ToECDSAPub returns ecdsa public key according the input data

func (*PublicKey) Bytes

func (pub *PublicKey) Bytes() []byte

Bytes returns the ecdsa PublickKey to bytes

type Signature

type Signature [SignatureSize]byte

Signature represents the ecdsa_signcompact signature data format [r - s - v]

func (*Signature) Bytes

func (sig *Signature) Bytes() []byte

Bytes returns the bytes of the signature

func (*Signature) Ecrecover

func (sig *Signature) Ecrecover(hash []byte) ([]byte, error)

Ecrecover recovers publick key

func (Signature) MarshalText

func (sig Signature) MarshalText() ([]byte, error)

MarshalText returns the hex representation of h.

func (*Signature) RecoverPublicKey

func (sig *Signature) RecoverPublicKey(hash []byte) (*PublicKey, error)

RecoverPublicKey recovers public key and also verifys the signature

func (*Signature) SetBytes

func (sig *Signature) SetBytes(data []byte, compressed bool)

SetBytes returns a format signature according the raw signature

func (*Signature) UnmarshalText

func (sig *Signature) UnmarshalText(input []byte) error

UnmarshalText parses a hash in hex syntax.

func (*Signature) VRS

func (sig *Signature) VRS() (v byte, r, s *big.Int)

VRS returns the v r s values

func (*Signature) Validate

func (sig *Signature) Validate() bool

Validate validates whether the signature values are valid

func (*Signature) Verify

func (sig *Signature) Verify(hash []byte, pub *PublicKey) bool

Verify verifys the signature with public key

Directories

Path Synopsis
sm2

Jump to

Keyboard shortcuts

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