How to use decryptPrivateKey method of lib Package

Best K6 code snippet using lib.decryptPrivateKey

encrypt.go

Source:encrypt.go Github

copy

Full Screen

1// Copyright 2021 ChainSafe Systems (ON)2// SPDX-License-Identifier: LGPL-3.0-only3package keystore4import (5 "crypto/aes"6 "crypto/cipher"7 "crypto/rand"8 "encoding/json"9 "errors"10 "fmt"11 "io"12 "os"13 "path/filepath"14 "github.com/ChainSafe/gossamer/lib/crypto"15 "github.com/ChainSafe/gossamer/lib/crypto/ed25519"16 "github.com/ChainSafe/gossamer/lib/crypto/secp256k1"17 "github.com/ChainSafe/gossamer/lib/crypto/sr25519"18 "golang.org/x/crypto/blake2b"19)20// EncryptedKeystore holds Type PublicKey and Ciphertext21type EncryptedKeystore struct {22 Type string23 PublicKey string24 Ciphertext []byte25}26// gcmFromPassphrase creates a symmetric AES key given a password27func gcmFromPassphrase(password []byte) (cipher.AEAD, error) {28 hash := blake2b.Sum256(password)29 block, err := aes.NewCipher(hash[:])30 if err != nil {31 return nil, err32 }33 gcm, err := cipher.NewGCM(block)34 if err != nil {35 return nil, err36 }37 return gcm, nil38}39// Encrypt uses AES to encrypt `msg` with the symmetric key deterministically created from `password`40func Encrypt(msg, password []byte) ([]byte, error) {41 gcm, err := gcmFromPassphrase(password)42 if err != nil {43 return nil, err44 }45 nonce := make([]byte, gcm.NonceSize())46 if _, err = io.ReadFull(rand.Reader, nonce); err != nil {47 return nil, err48 }49 ciphertext := gcm.Seal(nonce, nonce, msg, nil)50 return ciphertext, nil51}52// Decrypt uses AES to decrypt ciphertext with the symmetric key deterministically created from `password`53func Decrypt(data, password []byte) ([]byte, error) {54 gcm, err := gcmFromPassphrase(password)55 if err != nil {56 return nil, err57 }58 nonceSize := gcm.NonceSize()59 nonce, ciphertext := data[:nonceSize], data[nonceSize:]60 plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)61 if err != nil {62 return nil, err63 }64 return plaintext, nil65}66// EncryptPrivateKey uses AES to encrypt an encoded `crypto.PrivateKey` with a symmetric key deterministically67// created from `password`68func EncryptPrivateKey(pk crypto.PrivateKey, password []byte) ([]byte, error) {69 return Encrypt(pk.Encode(), password)70}71// DecryptPrivateKey uses AES to decrypt the ciphertext into a72// `crypto.PrivateKey` with a symmetric key deterministically73// created from `password`74func DecryptPrivateKey(data, password []byte, keytype string) (crypto.PrivateKey, error) {75 pk, err := Decrypt(data, password)76 if err != nil {77 return nil, err78 }79 return DecodePrivateKey(pk, keytype)80}81// EncryptAndWriteToFile encrypts the `crypto.PrivateKey` using the password and saves it to the specified file82func EncryptAndWriteToFile(path string, pk crypto.PrivateKey, password []byte) error {83 ciphertext, err := EncryptPrivateKey(pk, password)84 if err != nil {85 return err86 }87 pub, err := pk.Public()88 if err != nil {89 return fmt.Errorf("cannot get public key: %s", err)90 }91 keytype := ""92 if _, ok := pk.(*ed25519.PrivateKey); ok {93 keytype = crypto.Ed25519Type94 }95 if _, ok := pk.(*sr25519.PrivateKey); ok {96 keytype = crypto.Sr25519Type97 }98 if _, ok := pk.(*secp256k1.PrivateKey); ok {99 keytype = crypto.Secp256k1Type100 }101 if keytype == "" {102 return errors.New("cannot write key not of type sr25519, ed25519, secp256k1")103 }104 keydata := &EncryptedKeystore{105 Type: keytype,106 PublicKey: pub.Hex(),107 Ciphertext: ciphertext,108 }109 data, err := json.MarshalIndent(keydata, "", "\t")110 if err != nil {111 return err112 }113 err = os.WriteFile(filepath.Clean(path), append(data, byte('\n')), 0600)114 if err != nil {115 return fmt.Errorf("cannot write to destination file: %w", err)116 }117 return nil118}119// ReadFromFileAndDecrypt reads ciphertext from a file and decrypts it using the password into a `crypto.PrivateKey`120func ReadFromFileAndDecrypt(filename string, password []byte) (crypto.PrivateKey, error) {121 fp, err := filepath.Abs(filename)122 if err != nil {123 return nil, err124 }125 data, err := os.ReadFile(filepath.Clean(fp))126 if err != nil {127 return nil, err128 }129 keydata := new(EncryptedKeystore)130 err = json.Unmarshal(data, keydata)131 if err != nil {132 return nil, err133 }134 return DecryptPrivateKey(keydata.Ciphertext, password, keydata.Type)135}...

Full Screen

Full Screen

decryptPrivateKey

Using AI Code Generation

copy

Full Screen

1decryptPrivateKey(key, password)2decryptPrivateKey(key, password)3decryptPrivateKey(key, password)4decryptPrivateKey(key, password)5decryptPrivateKey(key, password)6decryptPrivateKey(key, password)7decryptPrivateKey(key, password)8decryptPrivateKey(key, password)9decryptPrivateKey(key, password)10decryptPrivateKey(key, password)11decryptPrivateKey(key, password)12decryptPrivateKey(key, password)13decryptPrivateKey(key, password)14decryptPrivateKey(key, password)15decryptPrivateKey(key, password)16decryptPrivateKey(key, password)17decryptPrivateKey(key, password)18decryptPrivateKey(key, password)19decryptPrivateKey(key, password)20decryptPrivateKey(key, password)21decryptPrivateKey(key, password)

Full Screen

Full Screen

decryptPrivateKey

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 key, err := crypto.HexToECDSA("0x")4 if err != nil {5 panic(err)6 }7 fmt.Println(key)8}9import (10func main() {11 key, err := crypto.HexToECDSA("0x")12 if err != nil {13 panic(err)14 }15 fmt.Println(key)16}17import (18func main() {19 key, err := crypto.HexToECDSA("0x")20 if err != nil {21 panic(err)22 }23 fmt.Println(key)24}25import (26func main() {27 key, err := crypto.HexToECDSA("0x")28 if err != nil {29 panic(err)30 }31 fmt.Println(key)32}33import (34func main() {35 key, err := crypto.HexToECDSA("0x")36 if err != nil {37 panic(err)38 }39 fmt.Println(key)40}41import (42func main() {43 key, err := crypto.HexToECDSA("0x")44 if err != nil {45 panic(err)46 }47 fmt.Println(key)48}49import (50func main() {51 key, err := crypto.HexToECDSA("0x")52 if err != nil {53 panic(err

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful