当前位置: 首页>>代码示例>>Golang>>正文


Golang sha512.New函数代码示例

本文整理汇总了Golang中crypto/sha512.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: NewAddress

func NewAddress(version byte) (*Address, error) {
	addr := new(Address)
	addr.Version = version

	var err error
	addr.PrivateKey, err = ecdsa.GenerateKey(ec256k1.S256(), rand.Reader)
	if err != nil {
		return nil, errors.New("address.New: Error generating ecdsa encryption key")
	}

	publicKey := append(addr.PrivateKey.PublicKey.X.Bytes(),
		addr.PrivateKey.PublicKey.Y.Bytes()...)

	sha := sha512.New()
	sha.Write(publicKey)

	ripemd := ripemd160.New()
	ripemd.Write(sha.Sum(nil))
	hash := ripemd.Sum(nil)

	toCheck := []byte{addr.Version}
	toCheck = append(toCheck, hash...)
	sha1, sha2 := sha512.New(), sha512.New()
	sha1.Write(toCheck)
	sha2.Write(sha1.Sum(nil))
	checksum := sha2.Sum(nil)[:4]

	addr.Base58, err = EncodeBase58(append(toCheck, checksum...))
	if err != nil {
		return nil, err
	}

	return addr, nil
}
开发者ID:jashper,项目名称:bitmask-go,代码行数:34,代码来源:address.go

示例2: ValidateNonce

func ValidateNonce(payload []byte) bool {

	if len(payload) < 2 {
		return false
	}

	var offset int
	var nonce, trials_test uint64
	var hash_test, initial_payload []byte

	nonce, offset = varint.Decode(payload)
	initial_payload = payload[offset:]

	sha := sha512.New()
	sha.Write(initial_payload)
	initial_hash := sha.Sum(nil)

	var target uint64 = MAX_UINT64 / uint64((len(payload)+PAYLOAD_LENGTH_EXTRA_BYTES+8)*AVERAGE_PROOF_OF_WORK_NONCE_TRIALS_PER_BYTE)

	hash_test = varint.Encode(nonce)
	hash_test = append(hash_test, initial_hash...)
	sha1, sha2 := sha512.New(), sha512.New()
	sha1.Write(hash_test)
	sha2.Write(sha1.Sum(nil))

	trials_test = binary.BigEndian.Uint64(sha2.Sum(nil)[:8])

	return trials_test <= target
}
开发者ID:sporkmonger,项目名称:bitmessage-go,代码行数:29,代码来源:pow.go

示例3: newHash

// newHash - gives you a newly allocated hash depending on the input algorithm.
func newHash(algo string) hash.Hash {
	switch algo {
	case "sha512":
		return sha512.New()
	// Add new hashes here.
	default:
		return sha512.New()
	}
}
开发者ID:yrashk,项目名称:minio,代码行数:10,代码来源:erasure-utils.go

示例4: main

// (cipher)^D mod N = plain
// plivate key = {D,N}
// D means 'Decription'
func main() {

	// Generate key pair. public key {E,N} and private key {D,N}
	// E is 64437 https://en.wikipedia.org/wiki/65537_(number))

	// size of key (bits)
	size := 2048

	// nprimes is the number of prime of which N consists
	// e.g., if nprimes is 2, N = p*q. If nprimes is 3, N = p*q*r
	nprimes := 2

	privateKey, err := rsa.GenerateKey(rand.Reader, size)
	if err != nil {
		fmt.Printf("err: %s", err)
		return
	}

	// N = p*q
	var z big.Int
	if privateKey.N.Cmp(z.Mul(privateKey.Primes[0], privateKey.Primes[1])) != 0 {
		panic("shoud not reach here")
	}

	plain := []byte("Bob loves Alice.")

	// A label is a byte string that is effectively bound to the ciphertext in a nonmalleable way.
	// http://crypto.stackexchange.com/questions/2074/rsa-oaep-input-parameters
	label := []byte("test")

	// Get public key from private key and encrypt
	publicKey := &privateKey.PublicKey
	cipherText, err := rsa.EncryptOAEP(sha512.New(), rand.Reader, publicKey, plain, label)
	if err != nil {
		fmt.Printf("Err: %s\n", err)
		return
	}
	fmt.Printf("Cipher: %x\n", cipherText)

	// Decrypt with private key
	plainText, err := rsa.DecryptOAEP(sha512.New(), rand.Reader, privateKey, cipherText, label)
	if err != nil {
		fmt.Printf("Err: %s\n", err)
		return
	}

	fmt.Printf("Plain: %s\n", plainText)
}
开发者ID:tcnksm,项目名称:go-crypto,代码行数:51,代码来源:main.go

示例5: CertificateToDANE

// CertificateToDANE converts a certificate to a hex string as used in the TLSA record.
func CertificateToDANE(selector, matchingType uint8, cert *x509.Certificate) string {
	switch matchingType {
	case 0:
		switch selector {
		case 0:
			return hex.EncodeToString(cert.Raw)
		case 1:
			return hex.EncodeToString(cert.RawSubjectPublicKeyInfo)
		}
	case 1:
		h := sha256.New()
		switch selector {
		case 0:
			return hex.EncodeToString(cert.Raw)
		case 1:
			io.WriteString(h, string(cert.RawSubjectPublicKeyInfo))
			return hex.EncodeToString(h.Sum(nil))
		}
	case 2:
		h := sha512.New()
		switch selector {
		case 0:
			return hex.EncodeToString(cert.Raw)
		case 1:
			io.WriteString(h, string(cert.RawSubjectPublicKeyInfo))
			return hex.EncodeToString(h.Sum(nil))
		}
	}
	return ""
}
开发者ID:ActiveState,项目名称:dns,代码行数:31,代码来源:tlsa.go

示例6: CertificateToDANE

// CertificateToDANE converts a certificate to a hex string as used in the TLSA record.
func CertificateToDANE(selector, matchingType uint8, cert *x509.Certificate) (string, error) {
	switch matchingType {
	case 0:
		switch selector {
		case 0:
			return hex.EncodeToString(cert.Raw), nil
		case 1:
			return hex.EncodeToString(cert.RawSubjectPublicKeyInfo), nil
		}
	case 1:
		h := sha256.New()
		switch selector {
		case 0:
			io.WriteString(h, string(cert.Raw))
			return hex.EncodeToString(h.Sum(nil)), nil
		case 1:
			io.WriteString(h, string(cert.RawSubjectPublicKeyInfo))
			return hex.EncodeToString(h.Sum(nil)), nil
		}
	case 2:
		h := sha512.New()
		switch selector {
		case 0:
			io.WriteString(h, string(cert.Raw))
			return hex.EncodeToString(h.Sum(nil)), nil
		case 1:
			io.WriteString(h, string(cert.RawSubjectPublicKeyInfo))
			return hex.EncodeToString(h.Sum(nil)), nil
		}
	}
	return "", errors.New("dns: bad TLSA MatchingType or TLSA Selector")
}
开发者ID:FlyWings,项目名称:kubernetes,代码行数:33,代码来源:tlsa.go

示例7: main

func main() {
	for _, h := range []hash.Hash{md4.New(), md5.New(), sha1.New(),
		sha256.New224(), sha256.New(), sha512.New384(), sha512.New(),
		ripemd160.New()} {
		fmt.Printf("%x\n\n", h.Sum())
	}
}
开发者ID:sbhackerspace,项目名称:sbhx-snippets,代码行数:7,代码来源:hash.go

示例8: NewVerifier

func NewVerifier(hashes map[string]string, size int64) (*Verifier, error) {
	if size <= 0 {
		return nil, &ErrInvalidSize{size}
	}
	v := &Verifier{
		hashes: make([]*Hash, 0, len(hashes)),
		size:   size,
	}
	for algorithm, value := range hashes {
		h := &Hash{algorithm: algorithm, expected: value}
		switch algorithm {
		case "sha256":
			h.hash = sha256.New()
		case "sha512":
			h.hash = sha512.New()
		case "sha512_256":
			h.hash = sha512.New512_256()
		default:
			continue
		}
		v.hashes = append(v.hashes, h)
	}
	if len(v.hashes) == 0 {
		return nil, ErrNoHashes
	}
	return v, nil
}
开发者ID:imjorge,项目名称:flynn,代码行数:27,代码来源:verify.go

示例9: StoreBlock

// Store takes a block and stores it in the Bucket using a
// Sha1-Hash as key, wich is returned
func StoreBlock(b Bucket, block []byte) (hash []byte, err error) {
	h := sha512.New()
	h.Write(block)
	hash = h.Sum([]byte{})
	err = b.Store(hash, block)
	return
}
开发者ID:postfix,项目名称:libblockify,代码行数:9,代码来源:functions.go

示例10: NewFileMeta

// NewFileMeta generates a FileMeta object from the reader, using the
// hash algorithms provided
func NewFileMeta(r io.Reader, hashAlgorithms ...string) (FileMeta, error) {
	if len(hashAlgorithms) == 0 {
		hashAlgorithms = []string{defaultHashAlgorithm}
	}
	hashes := make(map[string]hash.Hash, len(hashAlgorithms))
	for _, hashAlgorithm := range hashAlgorithms {
		var h hash.Hash
		switch hashAlgorithm {
		case "sha256":
			h = sha256.New()
		case "sha512":
			h = sha512.New()
		default:
			return FileMeta{}, fmt.Errorf("Unknown Hash Algorithm: %s", hashAlgorithm)
		}
		hashes[hashAlgorithm] = h
		r = io.TeeReader(r, h)
	}
	n, err := io.Copy(ioutil.Discard, r)
	if err != nil {
		return FileMeta{}, err
	}
	m := FileMeta{Length: n, Hashes: make(Hashes, len(hashes))}
	for hashAlgorithm, h := range hashes {
		m.Hashes[hashAlgorithm] = h.Sum(nil)
	}
	return m, nil
}
开发者ID:DaveDaCoda,项目名称:docker,代码行数:30,代码来源:types.go

示例11: computeDetachedDigest

func computeDetachedDigest(nonce []byte, plaintext []byte) []byte {
	hasher := sha512.New()
	hasher.Write(nonce)
	hasher.Write(plaintext)

	return detachedDigest(hasher.Sum(nil))
}
开发者ID:mark-adams,项目名称:client,代码行数:7,代码来源:common.go

示例12: main

func main() {
	privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
	if err != nil {
		fmt.Printf("rsa.GenerateKey: %v\n", err)
		return
	}

	message := "Hello World!"
	messageBytes := bytes.NewBufferString(message)
	hash := sha512.New()
	hash.Write(messageBytes.Bytes())
	digest := hash.Sum(nil)

	fmt.Printf("messageBytes: %v\n", messageBytes)
	fmt.Printf("hash: %V\n", hash)
	fmt.Printf("digest: %v\n", digest)

	signature, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA512, digest)
	if err != nil {
		fmt.Printf("rsa.SignPKCS1v15 error: %v\n", err)
		return
	}

	fmt.Printf("signature: %v\n", signature)

	err = rsa.VerifyPKCS1v15(&privateKey.PublicKey, crypto.SHA512, digest, signature)
	if err != nil {
		fmt.Printf("rsa.VerifyPKCS1v15 error: %V\n", err)
	}

	fmt.Println("Signature good!")
}
开发者ID:leepro,项目名称:goplay,代码行数:32,代码来源:rsasign.go

示例13: sha512

func (e *Engine) sha512() error {
	data, err := computeHash(sha512.New(), e.stack.Pop())
	if err == nil {
		e.stack.Push(data)
	}
	return err
}
开发者ID:ancientlore,项目名称:hashsrv,代码行数:7,代码来源:hash.go

示例14: TestCannedContent

// TestCannedContent exercises the input and output removing the randomness of rand
func TestCannedContent(t *testing.T) {
	b := bytes.NewBufferString(DilbertRandom)
	s := NewSuiteWithDev(t, b)
	defer s.TearDown()

	res, err := http.Get(s.URL + "?challenge=pork+chop+sandwiches")
	s.Assert(err == nil, "http client error:", err)
	defer res.Body.Close()
	chal, seed, err := ReadResp(res.Body)
	s.Assert(err == nil, "response error:", err)
	s.Assert(chal == PorkChopSha512, "expected:", PorkChopSha512, "got:", chal)
	s.SanityCheck(chal, seed)
	// Check that the 'random' seed we got back was appropriately mixed
	// with the challenge
	s.Assert(seed != DilbertRandom, "got the raw random content")
	s.Assert(seed != DilbertRandomSHA1, "got the sha of random content without the challenge")
	expectedSum := sha512.New()
	io.WriteString(expectedSum, "pork chop sandwiches")
	io.WriteString(expectedSum, DilbertRandom)
	expectedSeed := fmt.Sprintf("%x", expectedSum.Sum(nil))
	s.Assert(seed == expectedSeed, "expected:", expectedSeed, "got:", seed)
	// We can also check that the challenge was correctly written to our random device
	// b.Bytes() is the remainder of our buffer, and Buffer writes at the end
	// This also shows that we didn't write the raw request
	writtenBytesInHex := fmt.Sprintf("%x", string(b.Bytes()))
	s.Assert(PorkChopSha512 == writtenBytesInHex, "expected:", PorkChopSha512, "got:", writtenBytesInHex)
}
开发者ID:SUNET,项目名称:pollen,代码行数:28,代码来源:pollen_test.go

示例15: main

func main() {
	if len(os.Args) < 2 {
		return
	}

	p := os.Args[1]

	i := big.NewInt(0)
	o := big.NewInt(1)
	h := sha512.New()

	for true {
		m := i.Bytes()
		m = append(m, make([]byte, 64-len(m))...)

		h.Write(m[:])

		c := hex.EncodeToString(h.Sum(nil))

		if strings.HasPrefix(c, p) {
			fmt.Println(hex.EncodeToString(m))
			fmt.Println(c)
			return
		}

		i = i.Add(i, o)
		h.Reset()
	}
}
开发者ID:hecrj,项目名称:c,代码行数:29,代码来源:find_input.go


注:本文中的crypto/sha512.New函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。