func NewKeyWithType(input string) (KeyWithType, error) {
	parts := strings.Split(input, ":")
	if len(parts) != 2 {
		return KeyWithType{}, fmt.Errorf("key must be of the form <algorithm>:<key in base64>, was: %s", input)
	}

	keyBytes, err := base64.StdEncoding.DecodeString(parts[1])
	if err != nil {
		return KeyWithType{}, fmt.Errorf("failed to base64-decode key: %v", err)
	}

	if parts[0] == "RSA" {
		if privKey, err := RSAPrivateKeyFromBytes(keyBytes); err == nil {
			// legacy private key
			return privKey, nil
		} else if pubKey, err := RSAPublicKeyFromBytes(keyBytes); err == nil {
			// legacy public key
			return pubKey, nil
		}

		// could not parse legacy key
		return KeyWithType{}, fmt.Errorf("unable to parse legacy RSA key")
	}

	alg, err := ToKeyType(parts[0])
	if err != nil {
		return KeyWithType{}, err
	}
	return alg.Generator()(keyBytes)
}
// NewKeyWithType returns a new KeyWithType based on the provided string, which must be the serialized form of the key
// compatible with the format generated by ToSerializable. Returns an error if the provided input cannot be parsed as a
// KeyWithType.
//
// For backwards-compatibility, this function also supports deserializing RSA key values that were serialized using the
// "legacy" format. The legacy format for RSA keys was "RSA:<base64-encoded-public-or-private-key>". If the provided
// input is of this form, it will be attempted to be parsed as a private key and then as a public key. If either of
// these operations succeeds, the proper KeyWithType is returned. Otherwise, an error is returned. Note that, although
// legacy values can be read, the returned KeyWithType will be of the new form, and calling "ToSerializable" on the
// returned KeyWithType will return the new form. Serializing RSA keys in the old form is not supported.
NewKeyWithType returns a new KeyWithType based on the provided string, which must be the serialized form of the key
compatible with the format generated by ToSerializable. Returns an error if the provided input cannot be parsed as a
KeyWithType.

For backwards-compatibility, this function also supports deserializing RSA key values that were serialized using the
"legacy" format. The legacy format for RSA keys was "RSA:". If the provided
input is of this form, it will be attempted to be parsed as a private key and then as a public key. If either of
these operations succeeds, the proper KeyWithType is returned. Otherwise, an error is returned. Note that, although
legacy values can be read, the returned KeyWithType will be of the new form, and calling "ToSerializable" on the
returned KeyWithType will return the new form. Serializing RSA keys in the old form is not supported.
// NewKeyWithType returns a new KeyWithType based on the provided string which must be the serialized form of the key // compatible with the format generated by ToSerializable . Returns an error if the provided input cannot be parsed as a // KeyWithType . // // For backwards - compatibility this function also supports deserializing RSA key values that were serialized using the // " legacy " format . The legacy format for RSA keys was " RSA : <base64 - encoded - public - or - private - key > " . If the provided // input is of this form it will be attempted to be parsed as a private key and then as a public key . If either of // these operations succeeds the proper KeyWithType is returned . Otherwise an error is returned . Note that although // legacy values can be read the returned KeyWithType will be of the new form and calling " ToSerializable " on the // returned KeyWithType will return the new form . Serializing RSA keys in the old form is not supported .
func NewKeyWithType(input string) (KeyWithType, error) {
	parts := strings.Split(input, ":")
	if len(parts) != 2 {
		return KeyWithType{}, fmt.Errorf("key must be of the form <algorithm>:<key in base64>, was: %s", input)
	}
	keyBytes, err := base64.StdEncoding.DecodeString(parts[1])
	if err != nil {
		return KeyWithType{}, fmt.Errorf("failed to base64-decode key: %v", err)
	}
	if parts[0] == "RSA" {
		if privKey, err := RSAPrivateKeyFromBytes(keyBytes); err == nil {
			return privKey, nil
		} else if pubKey, err := RSAPublicKeyFromBytes(keyBytes); err == nil {
			return pubKey, nil
		}
		return KeyWithType{}, fmt.Errorf("unable to parse legacy RSA key")
	}
	alg, err := ToKeyType(parts[0])
	if err != nil {
		return KeyWithType{}, err
	}
	return alg.Generator()(keyBytes)
}
func NewKeyWithType ( input string ) ( KeyWithType , error ) { parts := strings . Split ( input , " " ) if len ( parts ) != 2 { return KeyWithType { } , fmt . Errorf ( " " , input ) } keyBytes , err := base64 . StdEncoding . DecodeString ( parts [ 1 ] ) if err != nil { return KeyWithType { } , fmt . Errorf ( " " , err ) } if parts [ 0 ] == " " { if privKey , err := RSAPrivateKeyFromBytes ( keyBytes ) ; err == nil { return privKey , nil } else if pubKey , err := RSAPublicKeyFromBytes ( keyBytes ) ; err == nil { return pubKey , nil } return KeyWithType { } , fmt . Errorf ( " " ) } alg , err := ToKeyType ( parts [ 0 ] ) if err != nil { return KeyWithType { } , err } return alg . Generator ( ) ( keyBytes ) }
NewKeyWithType returns a new KeyWithType based on the provided string, which must be the serialized form of the key
compatible with the format generated by ToSerializable.
NewKeyWithType returns a new KeyWithType based on the provided string which must be the serialized form of the key compatible with the format generated by ToSerializable .
// legacy private key // legacy public key // could not parse legacy key