/// <summary> /// Default constructor for PaymentCardSecret /// </summary> /// <param name="paymentCard">PaymentCard to encrypt</param> /// <param name="keyIdentifier">Key identifier</param> /// <param name="algorithm">Symmetric Key Algorithm used for encryption</param> /// <param name="derivedPassword">Derived password</param> public PaymentCardSecret(PaymentCard paymentCard, string keyIdentifier, SymmetricKeyAlgorithm algorithm, byte[] derivedPassword) { Dictionary <string, object> dictionaryForAUDALF = new Dictionary <string, object>() { { PaymentCard.titleKey, paymentCard.GetTitle() }, { PaymentCard.nameOnCardKey, paymentCard.GetNameOnCard() }, { PaymentCard.cardTypeKey, paymentCard.GetCardType() }, { PaymentCard.numberKey, paymentCard.GetNumber() }, { PaymentCard.securityCodeKey, paymentCard.GetSecurityCode() }, { PaymentCard.startDateKey, paymentCard.GetStartDate() }, { PaymentCard.expirationDateKey, paymentCard.GetExpirationDate() }, { PaymentCard.notesKey, paymentCard.GetNotes() }, { PaymentCard.creationTimeKey, DateTimeOffset.FromUnixTimeSeconds(paymentCard.creationTime) }, { PaymentCard.modificationTimeKey, DateTimeOffset.FromUnixTimeSeconds(paymentCard.modificationTime) }, }; this.keyIdentifier = Encoding.UTF8.GetBytes(keyIdentifier); this.algorithm = algorithm; // Create AUDALF payload from dictionary byte[] serializedBytes = AUDALF_Serialize.Serialize(dictionaryForAUDALF, valueTypes: null, serializationSettings: serializationSettings); // Encrypt the AUDALF payload with given algorithm this.audalfData = algorithm.EncryptBytes(serializedBytes, derivedPassword); // Calculate new checksum this.CalculateAndUpdateChecksum(); }
/// <summary> /// Deep copy existing PaymentCard to new PaymentCard /// </summary> /// <param name="copyThis">PaymentCard to copy</param> public PaymentCard(PaymentCard copyThis) { this.title = new byte[copyThis.title.Length]; Buffer.BlockCopy(copyThis.title, 0, this.title, 0, copyThis.title.Length); this.nameOnCard = new byte[copyThis.nameOnCard.Length]; Buffer.BlockCopy(copyThis.nameOnCard, 0, this.nameOnCard, 0, copyThis.nameOnCard.Length); this.cardType = new byte[copyThis.cardType.Length]; Buffer.BlockCopy(copyThis.cardType, 0, this.cardType, 0, copyThis.cardType.Length); this.number = new byte[copyThis.number.Length]; Buffer.BlockCopy(copyThis.number, 0, this.number, 0, copyThis.number.Length); this.securityCode = new byte[copyThis.securityCode.Length]; Buffer.BlockCopy(copyThis.securityCode, 0, this.securityCode, 0, copyThis.securityCode.Length); this.startDate = new byte[copyThis.startDate.Length]; Buffer.BlockCopy(copyThis.startDate, 0, this.startDate, 0, copyThis.startDate.Length); this.expirationDate = new byte[copyThis.expirationDate.Length]; Buffer.BlockCopy(copyThis.expirationDate, 0, this.expirationDate, 0, copyThis.expirationDate.Length); this.notes = new byte[copyThis.notes.Length]; Buffer.BlockCopy(copyThis.notes, 0, this.notes, 0, copyThis.notes.Length); this.creationTime = copyThis.creationTime; this.modificationTime = copyThis.modificationTime; this.checksum = copyThis.checksum; }
/// <summary> /// Get PaymentCard. Use this for situation where you want to convert secret -> non secret /// </summary> /// <param name="derivedPassword">Derived password</param> /// <returns>PaymentCard</returns> public PaymentCard GetPaymentCard(byte[] derivedPassword) { Dictionary <string, object> dict = this.GetPaymentCardAsDictionary(derivedPassword); PaymentCard returnValue = new PaymentCard((string)dict[PaymentCard.titleKey], (string)dict[PaymentCard.nameOnCardKey], (string)dict[PaymentCard.cardTypeKey], (string)dict[PaymentCard.numberKey], (string)dict[PaymentCard.securityCodeKey], (string)dict[PaymentCard.startDateKey], (string)dict[PaymentCard.expirationDateKey], (string)dict[PaymentCard.notesKey]); returnValue.creationTime = ((DateTimeOffset)dict[PaymentCard.creationTimeKey]).ToUnixTimeSeconds(); returnValue.modificationTime = ((DateTimeOffset)dict[PaymentCard.modificationTimeKey]).ToUnixTimeSeconds(); return(returnValue); }
/// <summary> /// Replace existing payment card in Common secret container with another one (basically for editing purposes) /// </summary> /// <param name="zeroBasedIndex">Zero based index of payment card secret that will be replaced</param> /// <param name="derivedPassword">Derived password</param> /// <param name="paymentCard">Payment card to add</param> /// <param name="keyIdentifier">Key identifier</param> /// <param name="algorithm">Symmetric Encryption Algorithm to use</param> /// <returns>Tuple that tells if add was success, and possible error</returns> public (bool success, string possibleError) ReplacePaymentCardSecret(int zeroBasedIndex, byte[] derivedPassword, PaymentCard paymentCard, string keyIdentifier, SymmetricEncryptionAlgorithm algorithm = SymmetricEncryptionAlgorithm.AES_CTR) { if (zeroBasedIndex < 0 || zeroBasedIndex >= this.paymentCardSecrets.Count) { return(false, $"Index {zeroBasedIndex} is out of bounds [0, {this.paymentCardSecrets.Count})"); } (bool checkResult, string possibleError) = MandatoryChecks(paymentCard, "PaymentCard", keyIdentifier, derivedPassword); if (!checkResult) { return(checkResult, possibleError); } SymmetricKeyAlgorithm ska = SymmetricKeyAlgorithm.GenerateNew(algorithm); this.paymentCardSecrets[zeroBasedIndex] = new PaymentCardSecret(paymentCard, keyIdentifier, ska, derivedPassword); return(success : true, possibleError : ""); }
/// <summary> /// Add payment card to Common secret container /// </summary> /// <param name="derivedPassword">Derived password</param> /// <param name="paymentCard">Payment card to add</param> /// <param name="keyIdentifier">Key identifier</param> /// <param name="algorithm">Symmetric Encryption Algorithm to use</param> /// <returns>Tuple that tells if add was success, and possible error</returns> public (bool success, string possibleError) AddPaymentCardSecret(byte[] derivedPassword, PaymentCard paymentCard, string keyIdentifier, SymmetricEncryptionAlgorithm algorithm = SymmetricEncryptionAlgorithm.AES_CTR) { (bool checkResult, string possibleError) = MandatoryChecks(paymentCard, "PaymentCard", keyIdentifier, derivedPassword); if (!checkResult) { return(checkResult, possibleError); } SymmetricKeyAlgorithm ska = SymmetricKeyAlgorithm.GenerateNew(algorithm); this.paymentCardSecrets.Add(new PaymentCardSecret(paymentCard, keyIdentifier, ska, derivedPassword)); return(success : true, possibleError : ""); }