This repository has been archived by the owner on Oct 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
/
NullXmlEncryptor.cs
65 lines (56 loc) · 2.33 KB
/
NullXmlEncryptor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Xml.Linq;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.DataProtection.XmlEncryption
{
/// <summary>
/// An <see cref="IXmlEncryptor"/> that encrypts XML elements with a null encryptor.
/// </summary>
public sealed class NullXmlEncryptor : IXmlEncryptor
{
private readonly ILogger _logger;
/// <summary>
/// Creates a new instance of <see cref="NullXmlEncryptor"/>.
/// </summary>
public NullXmlEncryptor()
: this(services: null)
{
}
/// <summary>
/// Creates a new instance of <see cref="NullXmlEncryptor"/>.
/// </summary>
/// <param name="services">An optional <see cref="IServiceProvider"/> to provide ancillary services.</param>
public NullXmlEncryptor(IServiceProvider services)
{
_logger = services.GetLogger<NullXmlEncryptor>();
}
/// <summary>
/// Encrypts the specified <see cref="XElement"/> with a null encryptor, i.e.,
/// by returning the original value of <paramref name="plaintextElement"/> unencrypted.
/// </summary>
/// <param name="plaintextElement">The plaintext to echo back.</param>
/// <returns>
/// An <see cref="EncryptedXmlInfo"/> that contains the null-encrypted value of
/// <paramref name="plaintextElement"/> along with information about how to
/// decrypt it.
/// </returns>
public EncryptedXmlInfo Encrypt(XElement plaintextElement)
{
if (plaintextElement == null)
{
throw new ArgumentNullException(nameof(plaintextElement));
}
_logger?.EncryptingUsingNullEncryptor();
// <unencryptedKey>
// <!-- This key is not encrypted. -->
// <plaintextElement />
// </unencryptedKey>
var newElement = new XElement("unencryptedKey",
new XComment(" This key is not encrypted. "),
new XElement(plaintextElement) /* copy ctor */);
return new EncryptedXmlInfo(newElement, typeof(NullXmlDecryptor));
}
}
}