public string Create(string plainText) { if (string.IsNullOrEmpty(plainText)) { throw new ArgumentException("Value cannot be null or empty.", nameof(plainText)); } var tokenStorageItem = new TokenStorageItem(); var salt = SaltUtil.GenerateSecureRandomSalt(); var encrypted = EncryptionProvider.Encrypt(plainText, KeyVaultUtil.GetSecureSecretFromVault(), salt); if (encrypted != null) { tokenStorageItem = new TokenStorageItem { Token = CreateTokenGuid(), SaltValue = salt, EcnryptedValue = encrypted, CreateDateTime = DateTime.UtcNow, UpdatedDateTime = DateTime.UtcNow }; } //write or append to json file WriteAppendDataItem(tokenStorageItem); return(tokenStorageItem.Token); }
/// <summary> /// Helper method to write the contents of the list of TokenStorageItem back to the file system. /// </summary> /// <param name="item">TokenStorageItem item to add to the list.</param> private void WriteAppendDataItem(TokenStorageItem item) { var data = LoadJsonFromFile(); data.Add(item); //convert to json string var json = JsonConvert.SerializeObject(data.ToArray(), Formatting.Indented); //write file, or overwrite if it already exists File.WriteAllText(FilePath, json); }