| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Diagnostics.CodeAnalysis; |
| | 8 | | using Microsoft.Azure.KeyVault.Cryptography.Algorithms; |
| | 9 | |
|
| | 10 | | namespace Microsoft.Azure.KeyVault.Cryptography |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Resolves algorithm name to implementations. |
| | 14 | | /// </summary> |
| | 15 | | public class AlgorithmResolver |
| | 16 | | { |
| | 17 | | static AlgorithmResolver() |
| | 18 | | { |
| 2 | 19 | | Default.AddAlgorithm( Aes128CbcHmacSha256.AlgorithmName, new Aes128CbcHmacSha256() ); |
| 2 | 20 | | Default.AddAlgorithm( Aes192CbcHmacSha384.AlgorithmName, new Aes192CbcHmacSha384() ); |
| 2 | 21 | | Default.AddAlgorithm( Aes256CbcHmacSha512.AlgorithmName, new Aes256CbcHmacSha512() ); |
| | 22 | |
|
| 2 | 23 | | Default.AddAlgorithm( Aes128Cbc.AlgorithmName, new Aes128Cbc() ); |
| 2 | 24 | | Default.AddAlgorithm( Aes192Cbc.AlgorithmName, new Aes192Cbc() ); |
| 2 | 25 | | Default.AddAlgorithm( Aes256Cbc.AlgorithmName, new Aes256Cbc() ); |
| | 26 | |
|
| 2 | 27 | | Default.AddAlgorithm( AesKw128.AlgorithmName, new AesKw128() ); |
| 2 | 28 | | Default.AddAlgorithm( AesKw192.AlgorithmName, new AesKw192() ); |
| 2 | 29 | | Default.AddAlgorithm( AesKw256.AlgorithmName, new AesKw256() ); |
| | 30 | |
|
| 2 | 31 | | Default.AddAlgorithm( Rsa15.AlgorithmName, new Rsa15() ); |
| 2 | 32 | | Default.AddAlgorithm( RsaOaep.AlgorithmName, new RsaOaep() ); |
| | 33 | |
|
| 2 | 34 | | Default.AddAlgorithm( Rs256.AlgorithmName, new Rs256() ); |
| | 35 | |
|
| | 36 | | #if FullNetFx |
| | 37 | | Default.AddAlgorithm( RsNull.AlgorithmName, new RsNull() ); |
| | 38 | | #endif |
| | 39 | |
|
| 2 | 40 | | Default.AddAlgorithm( Es256.AlgorithmName, new Es256() ); |
| 2 | 41 | | Default.AddAlgorithm( Es384.AlgorithmName, new Es384() ); |
| 2 | 42 | | Default.AddAlgorithm( Es512.AlgorithmName, new Es512() ); |
| 2 | 43 | | Default.AddAlgorithm( ES256K.AlgorithmName, new ES256K() ); |
| 2 | 44 | | } |
| | 45 | |
|
| | 46 | | [SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")] |
| 2 | 47 | | public static readonly AlgorithmResolver Default = new AlgorithmResolver(); |
| | 48 | |
|
| 2 | 49 | | private readonly Dictionary<string, Algorithm> _algorithms = new Dictionary<string, Algorithm>(); |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Returns the implementation for an algorithm name |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="algorithmName">The algorithm name</param> |
| | 55 | | /// <returns></returns> |
| | 56 | | public Algorithm this[ string algorithmName ] |
| | 57 | | { |
| 118 | 58 | | get { return _algorithms[algorithmName]; } |
| 0 | 59 | | set { _algorithms[algorithmName] = value; } |
| | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Adds an algorithm to the resolver |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="algorithmName">The algorithm name</param> |
| | 66 | | /// <param name="provider">The provider for the algorithm</param> |
| | 67 | | public void AddAlgorithm( string algorithmName, Algorithm provider ) |
| | 68 | | { |
| 32 | 69 | | if ( string.IsNullOrWhiteSpace( algorithmName ) ) |
| 0 | 70 | | throw new ArgumentNullException( nameof( algorithmName ) ); |
| | 71 | |
|
| 32 | 72 | | if ( provider == null ) |
| 0 | 73 | | throw new ArgumentNullException( nameof( provider ) ); |
| | 74 | |
|
| 32 | 75 | | _algorithms[algorithmName] = provider; |
| 32 | 76 | | } |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Removes an algorithm from the resolver |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="algorithmName">The algorithm name</param> |
| | 82 | | public void RemoveAlgorithm( string algorithmName ) |
| | 83 | | { |
| 0 | 84 | | _algorithms.Remove( algorithmName ); |
| 0 | 85 | | } |
| | 86 | | } |
| | 87 | | } |