Some ed25519 and key function docs
This commit is contained in:
parent
e6accf20e9
commit
3ed91b141c
|
@ -38,10 +38,16 @@ func FromCryptoPublicKey(key ed25519.PublicKey) PublicKey {
|
||||||
return PublicKey(key)
|
return PublicKey(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Public simply delegates to PublicKey() to satisfy crypto.Signer. This method
|
||||||
|
// does a bit more work than the traditional Go ed25519's private key's Public()
|
||||||
|
// method so developers are encouraged to reuse the result.
|
||||||
func (p PrivateKey) Public() crypto.PublicKey {
|
func (p PrivateKey) Public() crypto.PublicKey {
|
||||||
return p.PublicKey()
|
return p.PublicKey()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PublicKey generates a public key for this private key. This method does a bit
|
||||||
|
// more work than the traditional Go ed25519's private key's Public() method so
|
||||||
|
// developers are encouraged to reuse the result.
|
||||||
func (p PrivateKey) PublicKey() PublicKey {
|
func (p PrivateKey) PublicKey() PublicKey {
|
||||||
var A edwards25519.ExtendedGroupElement
|
var A edwards25519.ExtendedGroupElement
|
||||||
var hBytes [32]byte
|
var hBytes [32]byte
|
||||||
|
@ -52,6 +58,7 @@ func (p PrivateKey) PublicKey() PublicKey {
|
||||||
return publicKeyBytes[:]
|
return publicKeyBytes[:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sign is not yet implemented.
|
||||||
func (p PrivateKey) Sign(rand io.Reader, message []byte, opts crypto.SignerOpts) (signature []byte, err error) {
|
func (p PrivateKey) Sign(rand io.Reader, message []byte, opts crypto.SignerOpts) (signature []byte, err error) {
|
||||||
if opts.HashFunc() != crypto.Hash(0) {
|
if opts.HashFunc() != crypto.Hash(0) {
|
||||||
return nil, errors.New("ed25519: cannot sign hashed message")
|
return nil, errors.New("ed25519: cannot sign hashed message")
|
||||||
|
|
|
@ -15,6 +15,9 @@ import (
|
||||||
|
|
||||||
var serviceIDEncoding = base32.StdEncoding.WithPadding(base32.NoPadding)
|
var serviceIDEncoding = base32.StdEncoding.WithPadding(base32.NoPadding)
|
||||||
|
|
||||||
|
// OnionServiceIDFromPrivateKey generates the onion service ID from the given
|
||||||
|
// private key. This panics if the private key is not a crypto/*rsa.PrivateKey
|
||||||
|
// or github.com/cretz/bine/torutil/ed25519.PrivateKey.
|
||||||
func OnionServiceIDFromPrivateKey(key crypto.PrivateKey) string {
|
func OnionServiceIDFromPrivateKey(key crypto.PrivateKey) string {
|
||||||
switch k := key.(type) {
|
switch k := key.(type) {
|
||||||
case *rsa.PrivateKey:
|
case *rsa.PrivateKey:
|
||||||
|
@ -25,6 +28,9 @@ func OnionServiceIDFromPrivateKey(key crypto.PrivateKey) string {
|
||||||
panic(fmt.Sprintf("Unrecognized private key type: %T", key))
|
panic(fmt.Sprintf("Unrecognized private key type: %T", key))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnionServiceIDFromPublicKey generates the onion service ID from the given
|
||||||
|
// public key. This panics if the public key is not a crypto/*rsa.PublicKey or
|
||||||
|
// github.com/cretz/bine/torutil/ed25519.PublicKey.
|
||||||
func OnionServiceIDFromPublicKey(key crypto.PublicKey) string {
|
func OnionServiceIDFromPublicKey(key crypto.PublicKey) string {
|
||||||
switch k := key.(type) {
|
switch k := key.(type) {
|
||||||
case *rsa.PublicKey:
|
case *rsa.PublicKey:
|
||||||
|
@ -35,12 +41,16 @@ func OnionServiceIDFromPublicKey(key crypto.PublicKey) string {
|
||||||
panic(fmt.Sprintf("Unrecognized private key type: %T", key))
|
panic(fmt.Sprintf("Unrecognized private key type: %T", key))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnionServiceIDFromV2PublicKey generates a V2 service ID for the given
|
||||||
|
// RSA-1024 public key.
|
||||||
func OnionServiceIDFromV2PublicKey(key *rsa.PublicKey) string {
|
func OnionServiceIDFromV2PublicKey(key *rsa.PublicKey) string {
|
||||||
h := sha1.New()
|
h := sha1.New()
|
||||||
h.Write(x509.MarshalPKCS1PublicKey(key))
|
h.Write(x509.MarshalPKCS1PublicKey(key))
|
||||||
return strings.ToLower(serviceIDEncoding.EncodeToString(h.Sum(nil)[:10]))
|
return strings.ToLower(serviceIDEncoding.EncodeToString(h.Sum(nil)[:10]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnionServiceIDFromV3PublicKey generates a V3 service ID for the given
|
||||||
|
// ED25519 public key.
|
||||||
func OnionServiceIDFromV3PublicKey(key ed25519.PublicKey) string {
|
func OnionServiceIDFromV3PublicKey(key ed25519.PublicKey) string {
|
||||||
checkSum := sha3.Sum256(append(append([]byte(".onion checksum"), key...), 0x03))
|
checkSum := sha3.Sum256(append(append([]byte(".onion checksum"), key...), 0x03))
|
||||||
var keyBytes [35]byte
|
var keyBytes [35]byte
|
||||||
|
|
Loading…
Reference in New Issue