|
| 1 | +// Copyright 2020 ONIXLabs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Security.Cryptography; |
| 17 | + |
| 18 | +namespace OnixLabs.Security.Cryptography; |
| 19 | + |
| 20 | +public sealed partial class EcdsaPrivateKey |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// Imports the ECDSA cryptographic private key data in PKCS #8 format. |
| 24 | + /// </summary> |
| 25 | + /// <param name="data">The cryptographic private key data to import.</param> |
| 26 | + /// <returns>Returns a new <see cref="EcdsaPrivateKey"/> instance from the imported cryptographic private key data.</returns> |
| 27 | + public static EcdsaPrivateKey ImportPkcs8PrivateKey(ReadOnlySpan<byte> data) |
| 28 | + { |
| 29 | + return ImportPkcs8PrivateKey(data, out int _); |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Imports the ECDSA cryptographic private key data in PKCS #8 format. |
| 34 | + /// </summary> |
| 35 | + /// <param name="data">The cryptographic private key data to import.</param> |
| 36 | + /// <param name="bytesRead">The number of bytes read from the input data.</param> |
| 37 | + /// <returns>Returns a new <see cref="EcdsaPrivateKey"/> instance from the imported cryptographic private key data.</returns> |
| 38 | + public static EcdsaPrivateKey ImportPkcs8PrivateKey(ReadOnlySpan<byte> data, out int bytesRead) |
| 39 | + { |
| 40 | + using ECDsa key = ECDsa.Create(); |
| 41 | + key.ImportPkcs8PrivateKey(data, out bytesRead); |
| 42 | + byte[] keyData = key.ExportECPrivateKey(); |
| 43 | + return new EcdsaPrivateKey(keyData); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Imports the ECDSA cryptographic private key data in encrypted PKCS #8 format. |
| 48 | + /// </summary> |
| 49 | + /// <param name="data">The cryptographic private key data to import.</param> |
| 50 | + /// <param name="password">The password required for password based decryption.</param> |
| 51 | + /// <returns>Returns a new <see cref="EcdsaPrivateKey"/> instance from the imported cryptographic private key data.</returns> |
| 52 | + public static EcdsaPrivateKey ImportPkcs8PrivateKey(ReadOnlySpan<byte> data, ReadOnlySpan<char> password) |
| 53 | + { |
| 54 | + return ImportPkcs8PrivateKey(data, password, out int _); |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Imports the ECDSA cryptographic private key data in encrypted PKCS #8 format. |
| 59 | + /// </summary> |
| 60 | + /// <param name="data">The cryptographic private key data to import.</param> |
| 61 | + /// <param name="password">The password required for password based decryption.</param> |
| 62 | + /// <param name="bytesRead">The number of bytes read from the input data.</param> |
| 63 | + /// <returns>Returns a new <see cref="EcdsaPrivateKey"/> instance from the imported cryptographic private key data.</returns> |
| 64 | + public static EcdsaPrivateKey ImportPkcs8PrivateKey(ReadOnlySpan<byte> data, ReadOnlySpan<char> password, out int bytesRead) |
| 65 | + { |
| 66 | + using ECDsa key = ECDsa.Create(); |
| 67 | + key.ImportEncryptedPkcs8PrivateKey(password, data, out bytesRead); |
| 68 | + byte[] keyData = key.ExportECPrivateKey(); |
| 69 | + return new EcdsaPrivateKey(keyData); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Imports the key data into a new <see cref="ECDsa"/> instance. |
| 74 | + /// </summary> |
| 75 | + /// <returns>Returns a new <see cref="ECDsa"/> instance containing the imported key data.</returns> |
| 76 | + private ECDsa ImportKeyData() |
| 77 | + { |
| 78 | + ECDsa algorithm = ECDsa.Create(); |
| 79 | + algorithm.ImportECPrivateKey(KeyData, out int _); |
| 80 | + return algorithm; |
| 81 | + } |
| 82 | +} |
0 commit comments