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


Golang PrivateKey.Primes方法代码示例

本文整理汇总了Golang中crypto/rsa.PrivateKey.Primes方法的典型用法代码示例。如果您正苦于以下问题:Golang PrivateKey.Primes方法的具体用法?Golang PrivateKey.Primes怎么用?Golang PrivateKey.Primes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在crypto/rsa.PrivateKey的用法示例。


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

示例1: parseRSAPrivateKey

func (pk *PrivateKey) parseRSAPrivateKey(data []byte) (err error) {
	rsaPub := pk.PublicKey.PublicKey.(*rsa.PublicKey)
	rsaPriv := new(rsa.PrivateKey)
	rsaPriv.PublicKey = *rsaPub

	buf := bytes.NewBuffer(data)
	d, _, err := readMPI(buf)
	if err != nil {
		return
	}
	p, _, err := readMPI(buf)
	if err != nil {
		return
	}
	q, _, err := readMPI(buf)
	if err != nil {
		return
	}

	rsaPriv.D = new(big.Int).SetBytes(d)
	rsaPriv.Primes = make([]*big.Int, 2)
	rsaPriv.Primes[0] = new(big.Int).SetBytes(p)
	rsaPriv.Primes[1] = new(big.Int).SetBytes(q)
	if err := rsaPriv.Validate(); err != nil {
		return err
	}
	rsaPriv.Precompute()
	pk.PrivateKey = rsaPriv
	pk.Encrypted = false
	pk.encryptedData = nil

	return nil
}
开发者ID:dylanpoe,项目名称:golang.org,代码行数:33,代码来源:private_key.go

示例2: main

func main() {
	var primes []uint64 = prime.PrimeSieveBatch{BatchSize: 100}.GetPrimes(10000)
	var p, q uint64 = /*uint64(104723), uint64(104729) */ primes[len(primes)-2], primes[len(primes)-1]
	fmt.Println(p, q)
	var mod int64 = int64(p * q)
	fmt.Println(mod)
	var puk, prk = /*int(65537), uint64(10195862609) */ KeyGenerator1{}.KeyGen(p, q)
	fmt.Println(puk, prk)

	b := []byte("Hi")
	var pub rsa.PublicKey = rsa.PublicKey{N: big.NewInt(mod), E: puk}
	var priv rsa.PrivateKey = rsa.PrivateKey{PublicKey: pub}
	priv.D = big.NewInt(int64(prk))
	priv.Primes = []*big.Int{big.NewInt(int64(p)), big.NewInt(int64(q))}

	fmt.Println(len(b))
	fmt.Printf("% x\n", b)

	//h := md5.New()
	enc, erre := rsa.EncryptPKCS1v15(rand.Reader, &pub, b)
	//h.Reset()
	dec, errd := rsa.DecryptPKCS1v15(rand.Reader, &priv, b)

	//fmt.Println(64 - 11)
	fmt.Printf("%s\n", erre)
	fmt.Printf("%s\n", errd)
	fmt.Printf("% x\n", enc)
	fmt.Printf("% x\n", dec)
}
开发者ID:frankbryce,项目名称:crypto-explore,代码行数:29,代码来源:main.go

示例3: UnpackPrivateKey

// msg -> rsa
func UnpackPrivateKey(k *msgs.PrivateKey) *rsa.PrivateKey {
	var key rsa.PrivateKey
	key.PublicKey = *UnpackKey(k.PublicKey)
	key.D = new(big.Int)
	key.D.SetBytes(k.D)
	for _, p := range k.Primes {
		key.Primes = append(key.Primes, new(big.Int).SetBytes(p))
	}
	return &key
}
开发者ID:Craig-Macomber,项目名称:election,代码行数:11,代码来源:pack.go

示例4: ParsePKCS1PrivateKey

// ParsePKCS1PrivateKey returns an RSA private key from its ASN.1 PKCS#1 DER encoded form.
func ParsePKCS1PrivateKey(der []byte) (*rsa.PrivateKey, error) {
	var priv pkcs1PrivateKey
	rest, err := asn1.Unmarshal(der, &priv)
	if len(rest) > 0 {
		return nil, asn1.SyntaxError{Msg: "trailing data"}
	}
	if err != nil {
		return nil, err
	}

	if priv.Version > 1 {
		return nil, errors.New("x509: unsupported private key version")
	}

	if priv.N.Sign() <= 0 || priv.D.Sign() <= 0 || priv.P.Sign() <= 0 || priv.Q.Sign() <= 0 {
		return nil, errors.New("x509: private key contains zero or negative value")
	}

	key := new(rsa.PrivateKey)
	key.PublicKey = rsa.PublicKey{
		E: priv.E,
		N: priv.N,
	}

	key.D = priv.D
	key.Primes = make([]*big.Int, 2+len(priv.AdditionalPrimes))
	key.Primes[0] = priv.P
	key.Primes[1] = priv.Q
	for i, a := range priv.AdditionalPrimes {
		if a.Prime.Sign() <= 0 {
			return nil, errors.New("x509: private key contains zero or negative prime")
		}
		key.Primes[i+2] = a.Prime
		// We ignore the other two values because rsa will calculate
		// them as needed.
	}

	err = key.Validate()
	if err != nil {
		return nil, err
	}
	key.Precompute()

	return key, nil
}
开发者ID:Greentor,项目名称:go,代码行数:46,代码来源:pkcs1.go

示例5: readPrivateKeyRSA

// Read a private key (file) string and create a public key. Return the private key.
func (k *RR_DNSKEY) readPrivateKeyRSA(kv map[string]string) (PrivateKey, os.Error) {
	p := new(rsa.PrivateKey)
	p.Primes = []*big.Int{nil, nil}
	for k, v := range kv {
		switch k {
		case "modulus", "publicexponent", "privateexponent", "prime1", "prime2":
			v1, err := packBase64([]byte(v))
			if err != nil {
				return nil, err
			}
			switch k {
			case "modulus":
				p.PublicKey.N = big.NewInt(0)
				p.PublicKey.N.SetBytes(v1)
			case "publicexponent":
				i := big.NewInt(0)
				i.SetBytes(v1)
				p.PublicKey.E = int(i.Int64()) // int64 should be large enough
			case "privateexponent":
				p.D = big.NewInt(0)
				p.D.SetBytes(v1)
			case "prime1":
				p.Primes[0] = big.NewInt(0)
				p.Primes[0].SetBytes(v1)
			case "prime2":
				p.Primes[1] = big.NewInt(0)
				p.Primes[1].SetBytes(v1)
			}
		case "exponent1", "exponent2", "coefficient":
			// not used in Go (yet)
		case "created", "publish", "activate":
			// not used in Go (yet)
		}
	}
	return p, nil
}
开发者ID:andradeandrey,项目名称:godns,代码行数:37,代码来源:keygen.go

示例6: main

func main() {
	message := []byte("MODULUS")
	message_p := []byte("PRIME Pea")
	pb := make([]byte, 32)
	pb[0] = 0x80
	for i, c := range message {
		pb[i] |= c >> 7
		pb[i+1] |= c << 1
	}
	copy(pb[len(message)+2:], message_p)
	p, err := PrimeWithPrefix(pb, 512)
	if err != nil {
		panic(err)
	}

	qb := make([]byte, 33)
	message_q := []byte("PRIME Queue")
	qb[0] = 0x80
	copy(qb[len(message)+2:], message_q)
	q, err := PrimeWithPrefix(qb, 512)
	if err != nil {
		panic(err)
	}

	n := big.NewInt(0)
	n.Mul(p, q)

	// To calculate the modulus from the desired private key, we need simply to
	// invert it modulo (p-1)(q-1)
	phi_p := big.NewInt(-1)
	phi_p.Add(phi_p, p)
	phi_q := big.NewInt(-1)
	phi_q.Add(phi_q, q)
	phi := big.NewInt(0)
	phi.Mul(phi_p, phi_q)

	d := big.NewInt(0x10001)
	d.ModInverse(d, phi)

	private := new(rsa.PrivateKey)
	private.N = n
	private.E = 0x10001
	private.D = d
	private.Primes = []*big.Int{p, q}
	private.Precompute()

	rfckey := rfc3441PrivateKey{Version: 0,
		Modulus:         n,
		PublicExponent:  0x10001,
		PrivateExponent: d,
		Prime1:          p, Prime2: q,
		Exponent1:   private.Precomputed.Dp,
		Exponent2:   private.Precomputed.Dq,
		Coefficient: private.Precomputed.Qinv}

	priv_enc, err := asn1.Marshal(rfckey)
	if err != nil {
		panic(err)
	}

	b := pem.Block{Type: "RSA PRIVATE KEY", Bytes: priv_enc}
	pem.Encode(os.Stdout, &b)
}
开发者ID:thijzert,项目名称:sslprobe,代码行数:63,代码来源:kg.go

示例7: ReadPrivateKey

// Read a private key (file) string and create a public key. Return the private key.
func (k *RR_DNSKEY) ReadPrivateKey(q io.Reader) (PrivateKey, os.Error) {
	p := new(rsa.PrivateKey)
	p.Primes = []*big.Int{nil, nil}
	var left, right string
	r := line.NewReader(q, 300)
	line, _, err := r.ReadLine()
	for err == nil {
		n, _ := fmt.Sscanf(string(line), "%s %s+\n", &left, &right)
		if n > 0 {
			switch left {
			case "Private-key-format:":
				if right != "v1.3" {
					return nil, ErrPrivKey
				}
			case "Algorithm:":
				a, _ := strconv.Atoi(right)
				if a == 0 {
					return nil, ErrAlg
				}
				k.Algorithm = uint8(a)
			case "Modulus:", "PublicExponent:", "PrivateExponent:", "Prime1:", "Prime2:":
				v, err := packBase64([]byte(right))
				if err != nil {
					return nil, err
				}
				if left == "Modulus:" {
					p.PublicKey.N = big.NewInt(0)
					p.PublicKey.N.SetBytes(v)
				}
				if left == "PublicExponent:" {
					i := big.NewInt(0)
					i.SetBytes(v)
					// Int64 should be large enough
					p.PublicKey.E = int(i.Int64())
				}
				if left == "PrivateExponent:" {
					p.D = big.NewInt(0)
					p.D.SetBytes(v)
				}
				if left == "Prime1:" {
					p.Primes[0] = big.NewInt(0)
					p.Primes[0].SetBytes(v)
				}
				if left == "Prime2:" {
					p.Primes[1] = big.NewInt(0)
					p.Primes[1].SetBytes(v)
				}
			case "Exponent1:", "Exponent2:", "Coefficient:":
				/* not used in Go (yet) */
			case "Created:", "Publish:", "Activate:":
				/* not used in Go (yet) */
			default:
				return nil, ErrKey
			}
		}
		line, _, err = r.ReadLine()
	}
	if !k.setPublicKeyRSA(p.PublicKey.E, p.PublicKey.N) {
		return nil, ErrKey
	}
	return p, nil
}
开发者ID:elazarl,项目名称:godns,代码行数:63,代码来源:keygen.go


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