private RealmObjectBase FindByPKDynamic(Type type, object primaryKeyValue, PKType pkType) { switch (pkType) { case PKType.Int: long?castPKValue; if (primaryKeyValue == null) { castPKValue = null; } else { castPKValue = Convert.ToInt64(primaryKeyValue); } return(_realm.DynamicApi.Find(type.Name, castPKValue)); case PKType.String: return(_realm.DynamicApi.Find(type.Name, (string)primaryKeyValue)); case PKType.ObjectId: return(_realm.DynamicApi.Find(type.Name, (ObjectId?)primaryKeyValue)); case PKType.Guid: return(_realm.DynamicApi.Find(type.Name, (Guid?)primaryKeyValue)); default: throw new NotSupportedException($"Unsupported pk type: {pkType}"); } }
public async Task <string> GenerateProdutKey(PKType type, ApplicationVersion appV, Client client, TimeSpan?time = null) { try { var prods = await _db.ProductKeys.Select(p => p.Key).ToListAsync(); while (true) { var pk = ""; if (type == PKType.Normal) { pk = generatePkNormal(appV, client); } else if (type == PKType.Time) { pk = generatePkTime(appV, client, (TimeSpan)time); } else if (type == PKType.Trial) { pk = generatePkTrial(appV, client, (TimeSpan)time); } if (!prods.Contains(pk)) { return(pk); } } } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } }
public void FindByPrimaryKeyDynamicTests(Type type, object primaryKeyValue, PKType pkType) { var obj = (RealmObject)Activator.CreateInstance(type); var pkProperty = type.GetProperties().Single(p => p.GetCustomAttribute <PrimaryKeyAttribute>() != null); pkProperty.SetValue(obj, primaryKeyValue); _realm.Write(() => _realm.Add(obj)); var foundObj = FindByPKDynamic(type, primaryKeyValue, pkType); Assert.That(foundObj, Is.Not.Null); Assert.That(pkProperty.GetValue(foundObj), Is.EqualTo(primaryKeyValue)); }
private RealmObjectBase FindByPKGeneric(Type type, object primaryKeyValue, PKType pkType) { var genericArgument = pkType switch { PKType.Int => typeof(long?), PKType.String => typeof(string), PKType.ObjectId => typeof(ObjectId?), PKType.Guid => typeof(Guid?), _ => throw new NotSupportedException(), }; var genericMethod = _realm.GetType().GetMethod(nameof(Realm.Find), new[] { genericArgument }); if (pkType == PKType.Int && primaryKeyValue != null) { primaryKeyValue = Convert.ToInt64(primaryKeyValue); } return((RealmObjectBase)genericMethod.MakeGenericMethod(type).Invoke(_realm, new[] { primaryKeyValue })); }
/// <summary> /// Reads a RSA private key PKCS#1 or PKCS#8 format from file. /// Use openssl rsa -in inputfile -out outputfile to convert to PKCS#1 if you need to. /// </summary> /// <param name="privateKeyFilePath"></param> /// <param name="pkType">The type of key from the file: PKCS#1 or PKCS#8</param> /// <returns>The private key object</returns> static RSA ReadPrivateKeyFromFile(String privateKeyFilePath, PKType pkType) { var rsa = RSA.Create(); var privateKeyContent = File.ReadAllText(privateKeyFilePath, System.Text.Encoding.UTF8); privateKeyContent = privateKeyContent.Replace(pkType == PKType.PKCS1 ? Program.BEGIN_PKCS1_PRIVATE_KEY : Program.BEGIN_PKCS8_PRIVATE_KEY, ""); privateKeyContent = privateKeyContent.Replace(pkType == PKType.PKCS1 ? Program.END_PKCS1_PRIVATE_KEY : Program.END_PKCS8_PRIVATE_KEY, ""); var privateKeyDecoded = Convert.FromBase64String(privateKeyContent); if (pkType == PKType.PKCS1) { rsa.ImportRSAPrivateKey(privateKeyDecoded, out _); } else { rsa.ImportPkcs8PrivateKey(privateKeyDecoded, out _); } return(rsa); }
private void SetupProvisioningWindow(PKType pKType) { CardPkType = pKType; CardGeneratedKey = new NBitcoin.Key(); switch (pKType) { case PKType.BTC: Network = NBitcoin.Network.Main; break; case PKType.BTC_TestNet: Network = NBitcoin.Network.TestNet; break; case PKType.CUSTOM: CardGeneratedKey = null; Network = null; break; } if (CardGeneratedKey != null) { PublicKeyTextBox.Text = CardGeneratedKey.PubKey.GetAddress(Network).ToString(); PrivateKeyTextBox.Text = CardGeneratedKey.GetBitcoinSecret(Network).ToWif(); //HelperText.Text = "When setting up a Bitcoin address, a private key is automatically generated for you. You can replace the private key data with a seed phrase mnemonic or your own WIF address."; } else { PublicKeyTextBox.Text = String.Empty; PrivateKeyTextBox.Text = String.Empty; // HelperText.Text = "Public data is not encrypted and optional. Any data may be used in the Private Key textbox, as long as it is under 96 bytes."; } }
/// <summary> /// Takes in a passphrase, a salt and private key - stores the encrypted bits locally, publicKey is optional. /// TODO: This method will be deprecated sooner than later for a more secure alternative. /// </summary> public async void EncryptPrivateKeyData( PKType keyType, string passPhrase, byte[] privateKey, byte[] publicKey) { if (privateKey.Length > 96) { throw new Exception("Private Key length was larger than 96 bytes."); } byte[] dataToEncrypt = new byte[MAX_USER_PK]; for (int i = 0; i < MAX_USER_PK; ++i) { if (i < privateKey.Length) { dataToEncrypt[i] = privateKey[i]; } else { dataToEncrypt[i] = 0xFF; } } bchipVIDent[VID_PKLEN_ADDR] = (byte)privateKey.Length; // Quick and dirty int maxKeys = 256; // Generate keys: // This is the initial version and meant for speed. Eventually, we will force the // client to go through a large number of potential passfords as a form of POW string pass = passPhrase.ToString(); var potPasswords = Encryptor.GeneratePassword(pass, maxKeys); byte[] initialPassword = Encryptor.CalculateSha512(CryptographicBuffer.ConvertStringToBinary(pass, BinaryStringEncoding.Utf8).ToArray()).ToArray(); int generatedPin = Encryptor.GeneratePinCode(initialPassword, maxKeys); // Selected a key, Generate IV byte[] chosenPassword = potPasswords[generatedPin % potPasswords.Count]; // Nuke the salt *every* time byte[] rnd = new byte[SALT_MAX_DATA]; RandomNumberGenerator.Create().GetBytes(rnd); this.Salt = rnd; byte[] chosenSalt = Encryptor.GenerateSalt(this.Salt); this.privateKeyData = Encryptor.Encrypt(dataToEncrypt.AsBuffer(), chosenPassword.AsBuffer(), chosenSalt.AsBuffer()).ToArray(); this.PkType = keyType; byte[] publicKeyData = new byte[PUBKEY_MAX_DATA]; int pubKeyLen = 0; if (publicKey != null) { pubKeyLen = publicKey.Length; bchipVIDent[VID_PUKLEN_ADDR] = (byte)pubKeyLen; } else { bchipVIDent[VID_PUKLEN_ADDR] = 0; } for (int i = 0; i < publicKeyData.Length; ++i) { if (i < pubKeyLen) { publicKeyData[i] = publicKey[i]; } else { publicKeyData[i] = 0xFF; } } this.publicKeyData = publicKeyData; this.crcData = GetCardCheckSum(); }
public void CreateObject_WhenPKExists_ShouldFail(Type type, object primaryKeyValue, PKType _) { _realm.Write(() => _realm.DynamicApi.CreateObject(type.Name, primaryKeyValue)); Assert.That(() => { _realm.Write(() => _realm.DynamicApi.CreateObject(type.Name, primaryKeyValue)); }, Throws.TypeOf <RealmDuplicatePrimaryKeyValueException>()); }
public void FailToFindByPrimaryKeyDynamicTests(Type type, object primaryKeyValue, PKType pkType) { var foundObj = FindByPKDynamic(type, primaryKeyValue, pkType); Assert.That(foundObj, Is.Null); }
public void ManageObject_WhenPKExists_ShouldFail(Type type, object primaryKeyValue, PKType _) { var pkProperty = type.GetProperties().Single(p => p.GetCustomAttribute <PrimaryKeyAttribute>() != null); var first = (RealmObject)Activator.CreateInstance(type); pkProperty.SetValue(first, primaryKeyValue); _realm.Write(() => _realm.Add(first)); Assert.That(() => { var second = (RealmObject)Activator.CreateInstance(type); pkProperty.SetValue(second, primaryKeyValue); _realm.Write(() => _realm.Add(second)); }, Throws.TypeOf <RealmDuplicatePrimaryKeyValueException>()); }
public ProtocolAttribute(PKType type, NetContext context) { }