Skip to main content
Construct keys in another format to a key representation or import keys from a file.
MethodTypeDescription
PrivateKey.fromString(<privateKey>)StringConstructs a private key string to PrivateKey
PublicKey.fromString(<publicKey>)StringConstructs a public key string to PublicKey
PrivateKey.fromStringECDSA(<privateKey>)StringConstructs an ECDSA key from a private key string
PublicKey.fromStringECDSA(<publicKey>)StringConstructs an ECDSA public key from a public key string
PrivateKey.fromBytesECDSA(<privateKey>)byte[ ]Constructs an ECDSA key from a private key bytes
PublicKey.fromBytesECDSA(<publicKey>)byte[ ]Constructs an ECDSA public key from a public key bytes
PrivateKey.fromStringED25519(<privateKey>)StringConstructs an ED25519 key from a private key string
PublicKey.fromStringED25519(<publicKey>)StringConstructs an ED25519 public key from a public key string
PrivateKey.fromBytesED25519(<privateKey>)byte [ ]Constructs an ED25519 key from a private key bytes
PublicKey.fromBytesED25519(<publiceKey>)byte [ ]Constructs an ED25519 public key from a public key bytes
PrivateKey.fromBytes(<privateKey>)byte [ ]Constructs a private key from bytes to PrivateKey
PublicKey.fromBytes(<publicKey>)byte [ ]Contructs a public key from bytes to PublicKey
PrivateKey.fromPem(<pemEncoded>)StringParse a private key from a PEM encoded string
PrivateKey.fromPem(<encodedPem, password>)String, StringParse a private key from a PEM encoded string. The private key may be encrypted, e.g. if it was generated by OpenSSL.
PrivateKey.readPem(<pemfile>)ReaderParse a private key from a PEM encoded reader
PrivateKey.readPem(<pemFile, password>)Reader, StringParse a private key from a PEM encoded stream. The key may be encrypted, e.g. if it was generated by OpenSSL.
//Converts an ECDSA private key string to PrivateKey
PrivateKey privateKey = PrivateKey.fromStringECDSA("3030020100300706052b8104000a042204208776c6b831a1b61ac10dac0304a2843de4716f98b09147a169f41d7b4d48ad5");

//The public key associated with the private key
PublicKey publicKey = PublicKey.fromStringECDSA("3036301006072a8648ce3d020106052b8104000a032200029bba954cd41e8ab9f05a0b3b038b1c2c35b9bc16b4e38bc67a2e5ebab2fe72fb");
//Converts an ECDSA private key string to PrivateKey
const privateKey = PrivateKey.fromStringECDSA("3030020100300706052b8104000a042204208776c6b831a1b61ac10dac0304a2843de4716f98b09147a169f41d7b4d48ad5");

//The public key associated with the private key
const publicKey = PublicKey.fromStringECDSA("3036301006072a8648ce3d020106052b8104000a032200029bba954cd41e8ab9f05a0b3b038b1c2c35b9bc16b4e38bc67a2e5ebab2fe72fb");
//Converts an ECDSA private key string to PrivateKey
privateKey, err := hedera.PrivateKeyFromStringECDSA("3030020100300706052b8104000a042204208776c6b831a1b61ac10dac0304a2843de4716f98b09147a169f41d7b4d48ad5")

if err != nil {
		panic(err)
	}

//The public key associated with the private key
publicKey, err := hedera.PublicKeyFromStringECDSA("3036301006072a8648ce3d020106052b8104000a032200029bba954cd41e8ab9f05a0b3b038b1c2c35b9bc16b4e38bc67a2e5ebab2fe72fb")

if err != nil {
		panic(err)
	}
// Import ECDSA keys from strings
let private_key = PrivateKey::from_str("3030020100300706052b8104000a042204208776c6b831a1b61ac10dac0304a2843de4716f98b09147a169f41d7b4d48ad5");
let public_key = PublicKey::from_str("3036301006072a8648ce3d020106052b8104000a032200029bba954cd41e8ab9f05a0b3b038b1c2c35b9bc16b4e38bc67a2e5ebab2fe72fb");

// Import from PEM
let private_key = PrivateKey::from_pem("-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----");
// With password if encrypted
let private_key = PrivateKey::from_pem_with_password("-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----", "password");

println!("Imported private key: {:?}", private_key);
println!("Imported public key: {:?}", public_key);

// v0.34.0