Пример #1
0
// GetHash returns and instance of hash.Hash using options opts.
// If opts is nil then the default hash function is returned.
func (csp *impl) GetHash(opts bccsp.HashOpts) (h hash.Hash, err error) {
	if opts == nil {
		return csp.conf.hashFunction(), nil
	}

	switch opts.(type) {
	case *bccsp.SHAOpts:
		return csp.conf.hashFunction(), nil
	case *bccsp.SHA256Opts:
		return sha256.New(), nil
	case *bccsp.SHA384Opts:
		return sha512.New384(), nil
	case *bccsp.SHA3_256Opts:
		return sha3.New256(), nil
	case *bccsp.SHA3_384Opts:
		return sha3.New384(), nil
	default:
		return nil, fmt.Errorf("Algorithm not recognized [%s]", opts.Algorithm())
	}
}
Пример #2
0
// Hash hashes messages msg using options opts.
func (csp *impl) Hash(msg []byte, opts bccsp.HashOpts) (digest []byte, err error) {
	var h hash.Hash
	if opts == nil {
		h = csp.conf.hashFunction()
	} else {
		switch opts.(type) {
		case *bccsp.SHAOpts:
			h = csp.conf.hashFunction()
		case *bccsp.SHA256Opts:
			h = sha256.New()
		case *bccsp.SHA384Opts:
			h = sha512.New384()
		case *bccsp.SHA3_256Opts:
			h = sha3.New256()
		case *bccsp.SHA3_384Opts:
			h = sha3.New384()
		default:
			return nil, fmt.Errorf("Algorithm not recognized [%s]", opts.Algorithm())
		}
	}

	h.Write(msg)
	return h.Sum(nil), nil
}