/// <summary>Parses the <see cref="string"/> into its <see cref="EncryptionAlgorithm"/> representation.</summary> /// <param name="value"></param> /// <param name="algorithm"></param> public static bool TryParse(string?value, [NotNullWhen(true)] out EncryptionAlgorithm?algorithm) { switch (value) { case "A128CBC-HS256": algorithm = A128CbcHS256; goto Found; case "A192CBC-HS384": algorithm = A192CbcHS384; goto Found; case "A256CBC-HS512": algorithm = A256CbcHS512; goto Found; case "A128GCM": algorithm = A128Gcm; goto Found; case "A192GCM": algorithm = A192Gcm; goto Found; case "A256GCM": algorithm = A256Gcm; goto Found; } algorithm = null; return(false); Found: return(true); }
/// <summary> /// Determines whether two specified <see cref="EncryptionAlgorithm"/> objects have the same value. /// </summary> /// <param name="other"></param> /// <returns></returns> public bool Equals(EncryptionAlgorithm?other) { if (other is null) { return(false); } return(_id == other._id); }
/// <summary> /// Initializes a new instance of the AS2ValidationSettings class. /// </summary> public AS2ValidationSettings(bool?overrideMessageProperties = default(bool?), bool?encryptMessage = default(bool?), bool?signMessage = default(bool?), bool?compressMessage = default(bool?), bool?checkDuplicateMessage = default(bool?), int?interchangeDuplicatesValidityDays = default(int?), bool?checkCertificateRevocationListOnSend = default(bool?), bool?checkCertificateRevocationListOnReceive = default(bool?), EncryptionAlgorithm?encryptionAlgorithm = default(EncryptionAlgorithm?)) { OverrideMessageProperties = overrideMessageProperties; EncryptMessage = encryptMessage; SignMessage = signMessage; CompressMessage = compressMessage; CheckDuplicateMessage = checkDuplicateMessage; InterchangeDuplicatesValidityDays = interchangeDuplicatesValidityDays; CheckCertificateRevocationListOnSend = checkCertificateRevocationListOnSend; CheckCertificateRevocationListOnReceive = checkCertificateRevocationListOnReceive; EncryptionAlgorithm = encryptionAlgorithm; }
/// <summary>Parses the <see cref="ReadOnlySpan{T}"/> into its <see cref="EncryptionAlgorithm"/> representation.</summary> public static bool TryParse(ReadOnlySpan <byte> value, [NotNullWhen(true)] out EncryptionAlgorithm?algorithm) { if (value.Length == 13) { switch (IntegerMarshal.ReadUInt64(value)) { case _A128CBC_ when IntegerMarshal.ReadUInt64(value, 5) == _BC_HS256: algorithm = A128CbcHS256; goto Found; case _A192CBC_ when IntegerMarshal.ReadUInt64(value, 5) == _BC_HS384: algorithm = A192CbcHS384; goto Found; case _A256CBC_ when IntegerMarshal.ReadUInt64(value, 5) == _BC_HS512: algorithm = A256CbcHS512; goto Found; } } else if (value.Length == 7) { switch (IntegerMarshal.ReadUInt56(value)) { case _A128GCM: algorithm = A128Gcm; goto Found; case _A192GCM: algorithm = A192Gcm; goto Found; case _A256GCM: algorithm = A256Gcm; goto Found; } } #if NET5_0_OR_GREATER Unsafe.SkipInit(out algorithm); #else algorithm = default; #endif return(false); Found: return(true); }
/// <summary> /// Parse the current value of the <see cref="Utf8JsonReader"/> into its <see cref="EncryptionAlgorithm"/> representation. /// </summary> /// <param name="reader"></param> /// <param name="algorithm"></param> public static bool TryParseSlow(ref Utf8JsonReader reader, [NotNullWhen(true)] out EncryptionAlgorithm?algorithm) { var algorithms = _algorithms; for (int i = 0; i < algorithms.Length; i++) { if (reader.ValueTextEquals(algorithms[i]._utf8Name)) { algorithm = algorithms[i]; return(true); } } algorithm = null; return(false); }
/// <summary>Parses the <see cref="ReadOnlySpan{T}"/> into its <see cref="EncryptionAlgorithm"/> representation.</summary> /// <param name="value"></param> /// <param name="algorithm"></param> public static bool TryParse(ReadOnlySpan <byte> value, [NotNullWhen(true)] out EncryptionAlgorithm?algorithm) { if (value.Length == 13) { switch (IntegerMarshal.ReadUInt64(value)) { case _A128CBC_ when IntegerMarshal.ReadUInt64(value, 5) == _BC_HS256: algorithm = A128CbcHS256; goto Found; case _A192CBC_ when IntegerMarshal.ReadUInt64(value, 5) == _BC_HS384: algorithm = A192CbcHS384; goto Found; case _A256CBC_ when IntegerMarshal.ReadUInt64(value, 5) == _BC_HS512: algorithm = A256CbcHS512; goto Found; } } else if (value.Length == 7) { switch (IntegerMarshal.ReadUInt56(value)) { case _A128GCM: algorithm = A128Gcm; goto Found; case _A192GCM: algorithm = A192Gcm; goto Found; case _A256GCM: algorithm = A256Gcm; goto Found; } } algorithm = null; return(false); Found: return(true); }
/// <summary> /// Defines the <see cref="Jwk"/> used as key for encryption. /// </summary> /// <param name="key"></param> /// <param name="encryptionAlgorithm"></param> /// <param name="keyManagementAlgorithm"></param> /// <returns></returns> public JwtDescriptorBuilder EncryptWith(Jwk key, EncryptionAlgorithm encryptionAlgorithm, KeyManagementAlgorithm?keyManagementAlgorithm) { if (key is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key); } if (encryptionAlgorithm is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.encryptionAlgorithm); } _encryptionKey = key; _keyManagementAlgorithm = keyManagementAlgorithm; _encryptionAlgorithm = encryptionAlgorithm; return(this); }
/// <summary>Parses the <see cref="JsonElement"/> into its <see cref="EncryptionAlgorithm"/> representation.</summary> public static bool TryParse(JsonElement value, [NotNullWhen(true)] out EncryptionAlgorithm?algorithm) { bool found; if (value.ValueEquals(A128CbcHS256._utf8Name.EncodedUtf8Bytes)) { algorithm = A128CbcHS256; found = true; } else if (value.ValueEquals(A192CbcHS384._utf8Name.EncodedUtf8Bytes)) { algorithm = A192CbcHS384; found = true; } else if (value.ValueEquals(A256CbcHS512._utf8Name.EncodedUtf8Bytes)) { algorithm = A256CbcHS512; found = true; } else if (value.ValueEquals(A128Gcm._utf8Name.EncodedUtf8Bytes)) { algorithm = A128Gcm; found = true; } else if (value.ValueEquals(A192Gcm._utf8Name.EncodedUtf8Bytes)) { algorithm = A192Gcm; found = true; } else if (value.ValueEquals(A256Gcm._utf8Name.EncodedUtf8Bytes)) { algorithm = A256Gcm; found = true; } else { #if NET5_0_OR_GREATER Unsafe.SkipInit(out algorithm); #else algorithm = default; #endif found = false; } return(found); }
/// <summary>Parses the <see cref="JsonElement"/> into its <see cref="EncryptionAlgorithm"/> representation.</summary> /// <param name="value"></param> /// <param name="algorithm"></param> public static bool TryParse(JsonElement value, [NotNullWhen(true)] out EncryptionAlgorithm?algorithm) { bool found; if (value.ValueEquals(A128CbcHS256._utf8Name.EncodedUtf8Bytes)) { algorithm = A128CbcHS256; found = true; } else if (value.ValueEquals(A192CbcHS384._utf8Name.EncodedUtf8Bytes)) { algorithm = A192CbcHS384; found = true; } else if (value.ValueEquals(A256CbcHS512._utf8Name.EncodedUtf8Bytes)) { algorithm = A256CbcHS512; found = true; } else if (value.ValueEquals(A128Gcm._utf8Name.EncodedUtf8Bytes)) { algorithm = A128Gcm; found = true; } else if (value.ValueEquals(A192Gcm._utf8Name.EncodedUtf8Bytes)) { algorithm = A192Gcm; found = true; } else if (value.ValueEquals(A256Gcm._utf8Name.EncodedUtf8Bytes)) { algorithm = A256Gcm; found = true; } else { algorithm = null; found = false; } return(found); }
/// <summary>Parses the current value of the <see cref="Utf8JsonReader"/> into its <see cref="EncryptionAlgorithm"/> representation.</summary> public static bool TryParseSlow(ref Utf8JsonReader reader, [NotNullWhen(true)] out EncryptionAlgorithm?algorithm) { var algorithms = _algorithms; for (int i = 0; i < algorithms.Length; i++) { if (reader.ValueTextEquals(algorithms[i]._utf8Name.EncodedUtf8Bytes)) { algorithm = algorithms[i]; return(true); } } #if NET5_0_OR_GREATER Unsafe.SkipInit(out algorithm); #else algorithm = default; #endif return(false); }
/// <summary> /// Cast the <see cref="ReadOnlySpan{T}"/> into its <see cref="EncryptionAlgorithm"/> representation. /// </summary> /// <param name="value"></param> /// <param name="algorithm"></param> public static bool TryParse(ReadOnlySpan <byte> value, [NotNullWhen(true)] out EncryptionAlgorithm?algorithm) { if (value.Length == 13) { ref byte refValue = ref MemoryMarshal.GetReference(value); ulong endValue = IntegerMarshal.ReadUInt64(ref refValue, 5); switch (IntegerMarshal.ReadUInt64(ref refValue)) { case A128CBC_ when endValue == BC_HS256: algorithm = Aes128CbcHmacSha256; return(true); case A192CBC_ when endValue == BC_HS384: algorithm = Aes192CbcHmacSha384; return(true); case A256CBC_ when endValue == BC_HS512: algorithm = Aes256CbcHmacSha512; return(true); } }
/// <summary>Parses the <see cref="string"/> into its <see cref="EncryptionAlgorithm"/> representation.</summary> public static bool TryParse(string?value, [NotNullWhen(true)] out EncryptionAlgorithm?algorithm) { switch (value) { case "A128CBC-HS256": algorithm = A128CbcHS256; goto Found; case "A192CBC-HS384": algorithm = A192CbcHS384; goto Found; case "A256CBC-HS512": algorithm = A256CbcHS512; goto Found; case "A128GCM": algorithm = A128Gcm; goto Found; case "A192GCM": algorithm = A192Gcm; goto Found; case "A256GCM": algorithm = A256Gcm; goto Found; } #if NET5_0_OR_GREATER Unsafe.SkipInit(out algorithm); #else algorithm = default; #endif return(false); Found: return(true); }
internal static Exception CreateNotSupportedException_EncryptionAlgorithm(EncryptionAlgorithm?algorithm) => new NotSupportedException($"Encryption failed. No support for: Algorithm: '{algorithm}'.");
internal static void ThrowNotSupportedException_EncryptionAlgorithm(EncryptionAlgorithm?algorithm) => throw CreateNotSupportedException_EncryptionAlgorithm(algorithm);
internal static string ToSerializedValue(this EncryptionAlgorithm?value) { return(value == null ? null : ((EncryptionAlgorithm)value).ToSerializedValue()); }
internal static void ThrowCryptographicException_EncryptionFailed(EncryptionAlgorithm?algorithm, Jwk key, Exception innerException) => throw CreateCryptographicException_EncryptionFailed(algorithm, key, innerException);
internal static void ThrowNotSupportedException_AlgorithmForKeyWrap(EncryptionAlgorithm?algorithm, Jwk key) => throw CreateNotSupportedException_AlgorithmForKeyWrap(algorithm, key);
private static Exception CreateNotSupportedException_AlgorithmForKeyWrap(EncryptionAlgorithm?algorithm) => new NotSupportedException($"Key wrap is not supported for algorithm: '{algorithm}'.");
private static Exception CreateCryptographicException_EncryptionFailed(EncryptionAlgorithm?algorithm, Jwk key, Exception innerException) => new CryptographicException($"Encryption failed for: Algorithm: '{algorithm}', key: '{key.Kid}'. See inner exception.", innerException);
/// <summary>Determines whether two specified <see cref="EncryptionAlgorithm"/> objects have the same value.</summary> public bool Equals(EncryptionAlgorithm?other) => !(other is null) && _id == other._id;
private static Exception CreateNotSupportedException_AlgorithmForKeyWrap(EncryptionAlgorithm?algorithm, Jwk key) => new NotSupportedException($"Key wrap is not supported for algorithm: '{algorithm}' with a key of type '{key.Kty}' and size of {key.KeySizeInBits} bits.");
public static string?MapAlogrithm(this EncryptionAlgorithm?algorithm) => algorithm switch { null => null, { } algo => MapAlogrithm(algo)