| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file | 3 // found in the LICENSE file |
| 4 | 4 |
| 5 package main | 5 package main |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "crypto/ecdsa" | 8 "crypto/ecdsa" |
| 9 "crypto/elliptic" | |
| 10 "crypto/md5" | 9 "crypto/md5" |
| 11 "crypto/rand" | |
| 12 "crypto/x509" | 10 "crypto/x509" |
| 13 "encoding" | 11 "encoding" |
| 14 "errors" | 12 "errors" |
| 15 "fmt" | 13 "fmt" |
| 16 ) | 14 ) |
| 17 | 15 |
| 18 // hash identifies a cryptographic hash function approved for use in signature a
lgorithms. | 16 // hash identifies a cryptographic hash function approved for use in signature a
lgorithms. |
| 19 type hash string | 17 type hash string |
| 20 | 18 |
| 21 const ( | 19 const ( |
| 22 sha1Hash = hash("SHA1") // sha1 cryptographic hash function defined
in RFC3174. | 20 sha1Hash = hash("SHA1") // sha1 cryptographic hash function defined
in RFC3174. |
| 23 sha256Hash = hash("SHA256") // sha256 cryptographic hash function define
d in FIPS 180-4. | 21 sha256Hash = hash("SHA256") // sha256 cryptographic hash function define
d in FIPS 180-4. |
| 24 sha384Hash = hash("SHA384") // sha384 cryptographic hash function define
d in FIPS 180-2. | 22 sha384Hash = hash("SHA384") // sha384 cryptographic hash function define
d in FIPS 180-2. |
| 25 sha512Hash = hash("SHA512") // sha512 cryptographic hash function define
d in FIPS 180-2. | 23 sha512Hash = hash("SHA512") // sha512 cryptographic hash function define
d in FIPS 180-2. |
| 26 ) | 24 ) |
| 27 | 25 |
| 28 // newPrincipalKey generates an ECDSA (public, private) key pair. | |
| 29 func newPrincipalKey() (publicKey, *ecdsa.PrivateKey, error) { | |
| 30 priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | |
| 31 if err != nil { | |
| 32 return nil, nil, err | |
| 33 } | |
| 34 return newECDSAPublicKey(&priv.PublicKey), priv, nil | |
| 35 } | |
| 36 | |
| 37 // publicKey represents a public key using an unspecified algorithm. | 26 // publicKey represents a public key using an unspecified algorithm. |
| 38 // | 27 // |
| 39 // MarshalBinary returns the DER-encoded PKIX representation of the public key, | 28 // MarshalBinary returns the DER-encoded PKIX representation of the public key, |
| 40 // while UnmarshalPublicKey creates a PublicKey object from the marshaled bytes. | 29 // while UnmarshalPublicKey creates a PublicKey object from the marshaled bytes. |
| 41 // | 30 // |
| 42 // String returns a human-readable representation of the public key. | 31 // String returns a human-readable representation of the public key. |
| 43 type publicKey interface { | 32 type publicKey interface { |
| 44 encoding.BinaryMarshaler | 33 encoding.BinaryMarshaler |
| 45 fmt.Stringer | 34 fmt.Stringer |
| 46 | 35 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 return &ecdsaPublicKey{v}, nil | 89 return &ecdsaPublicKey{v}, nil |
| 101 default: | 90 default: |
| 102 return nil, errors.New(fmt.Sprintf("Unrecognized key: %T", key)) | 91 return nil, errors.New(fmt.Sprintf("Unrecognized key: %T", key)) |
| 103 } | 92 } |
| 104 } | 93 } |
| 105 | 94 |
| 106 // newECDSAPublicKey creates a publicKey object that uses the ECDSA algorithm an
d the provided ECDSA public key. | 95 // newECDSAPublicKey creates a publicKey object that uses the ECDSA algorithm an
d the provided ECDSA public key. |
| 107 func newECDSAPublicKey(key *ecdsa.PublicKey) publicKey { | 96 func newECDSAPublicKey(key *ecdsa.PublicKey) publicKey { |
| 108 return &ecdsaPublicKey{key} | 97 return &ecdsaPublicKey{key} |
| 109 } | 98 } |
| OLD | NEW |