コード例 #1
0
        public static BaseKeyPair CreateBaseKeyPair(RawKeyPair rawKeyPair)
        {
            string privateKey = Hex.ToHexString(rawKeyPair.PrivateKey) + Hex.ToHexString(rawKeyPair.PublicKey);
            string publicKey  = EncodeCheck(rawKeyPair.PublicKey, Constants.ApiIdentifiers.ACCOUNT_PUBKEY);

            return(new BaseKeyPair(publicKey, privateKey));
        }
コード例 #2
0
        public void EncryptPassword()
        {
            RawKeyPair keyPair  = RawKeyPair.Generate();
            string     password = "******";

            byte[] privateBinary   = keyPair.ConcatenatedPrivateKey;
            byte[] encryptedBinary = client.EncryptPrivateKey(password, privateBinary);
            byte[] decryptedBinary = client.DecryptPrivateKey(password, encryptedBinary);
            CollectionAssert.AreEqual(privateBinary, decryptedBinary);
            byte[] publicBinary = keyPair.PublicKey;
            encryptedBinary = client.EncryptPublicKey(password, publicBinary);
            decryptedBinary = client.DecryptPublicKey(password, encryptedBinary);
            CollectionAssert.AreEqual(publicBinary, decryptedBinary);
        }
コード例 #3
0
ファイル: Keystore.cs プロジェクト: block-m3/aepp-sdk-net
        public static Keystore Generate(Configuration cfg, RawKeyPair rawKeyPair, string walletPassword, string walletName)
        {
            // create derived key with Argon2

            byte[] salt = Utils.Crypto.GenerateSalt(cfg.DefaultSaltLength);

            Argon2 arg = Utils.Crypto.GetArgon2FromType(cfg.Argon2Mode, walletPassword);

            arg.DegreeOfParallelism = cfg.Parallelism;
            arg.Iterations          = cfg.OpsLimit;
            arg.MemorySize          = cfg.MemLimitKIB;
            arg.Salt = salt;
            byte[] rawHash = arg.GetBytes(32);

            // chain public and private key byte arrays
            byte[] privateAndPublicKey = rawKeyPair.ConcatenatedPrivateKey;
            // encrypt the key arrays with nonce and derived key
            byte[] nonce      = SecretBox.GenerateNonce();
            byte[] ciphertext = SecretBox.Create(privateAndPublicKey, nonce, rawHash);

            // generate walletName if not given
            if (walletName == null || walletName.Trim().Length == 0)
            {
                walletName = "generated wallet file - " + DateTime.Now;
            }

            // generate the domain object for keystore
            Keystore wallet = new Keystore
            {
                PublicKey = rawKeyPair.GetPublicKey(),
                Id        = Guid.NewGuid().ToString().Replace("-", ""),
                Name      = walletName,
                Version   = cfg.Version,
                Crypto    = new Crypto
                {
                    SecretType         = cfg.SecretType,
                    SymmetricAlgorithm = cfg.SymmetricAlgorithm,
                    CipherText         = Hex.ToHexString(ciphertext),
                    Kdf          = cfg.Argon2Mode,
                    CipherParams = new CipherParams {
                        Nonce = Hex.ToHexString(nonce)
                    },
                    KdfParams = new KdfParams {
                        MemLimitKib = cfg.MemLimitKIB, OpsLimit = cfg.OpsLimit, Salt = Hex.ToHexString(salt), Parallelism = cfg.Parallelism
                    }
                }
            };

            return(wallet);
        }
コード例 #4
0
        public void Recover()
        {
            string walletFileSecret = "my_super_safe_password";

            Configuration cfg = ServiceProvider.GetService <Configuration>();
            // generate Keypair
            RawKeyPair keypair = RawKeyPair.Generate();
            Keystore   stor    = Keystore.Generate(cfg, keypair, walletFileSecret, null);

            Assert.IsNotNull(stor);

            // recover Keypair
            byte[]     recoveredPrivateKey = stor.RecoverPrivateKey(cfg.Argon2Mode, walletFileSecret);
            RawKeyPair recoveredRawKeypair = new RawKeyPair(Hex.ToHexString(recoveredPrivateKey));

            Assert.IsNotNull(recoveredRawKeypair);

            // compare generated and recovered keypair
            CollectionAssert.AreEqual(keypair.ConcatenatedPrivateKey, recoveredRawKeypair.ConcatenatedPrivateKey);
        }
コード例 #5
0
 public RawKeyPair EncryptRawKeyPair(RawKeyPair keyPairRaw, string password)
 {
     byte[] encryptedPublicKey  = EncryptPublicKey(password, keyPairRaw.PublicKey);
     byte[] encryptedPrivateKey = EncryptPrivateKey(password, keyPairRaw.PrivateKey);
     return(new RawKeyPair(encryptedPublicKey, encryptedPrivateKey));
 }
コード例 #6
0
        //KeyStore

        public Keystore GenerateKeystore(RawKeyPair rawKeyPair, string walletPassword, string walletName) => Keystore.Generate(Configuration, rawKeyPair, walletPassword, walletName);