From a23ef86d831e548258113d0877c6d87487304318 Mon Sep 17 00:00:00 2001 From: Aliwoto Date: Fri, 1 Oct 2021 10:10:13 +0000 Subject: [PATCH] Fix `JsonPropertyName` problem in `SessionIdentification` class; Fix code style problems in `Socialvoid.Security.Otp` namespace. Signed-off-by: Aliwoto --- Socialvoid/Client/SocialvoidClient.cs | 1 - Socialvoid/Security/Otp/Base32Encoding.cs | 22 ++++++++++---------- Socialvoid/Security/Otp/Hotp.cs | 4 ++-- Socialvoid/Security/Otp/InMemoryKey.cs | 10 +++++---- Socialvoid/Security/SessionIdentification.cs | 5 +++++ 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/Socialvoid/Client/SocialvoidClient.cs b/Socialvoid/Client/SocialvoidClient.cs index a2505d6..c41694b 100644 --- a/Socialvoid/Client/SocialvoidClient.cs +++ b/Socialvoid/Client/SocialvoidClient.cs @@ -399,7 +399,6 @@ namespace Socialvoid.Client { var otp = new Totp(Encoding.UTF8.GetBytes(secret)); return KeyGeneration.GetSha1(otp.ComputeTotp() + PrivateHash);; - //return null; } #endregion diff --git a/Socialvoid/Security/Otp/Base32Encoding.cs b/Socialvoid/Security/Otp/Base32Encoding.cs index d880eef..a807650 100644 --- a/Socialvoid/Security/Otp/Base32Encoding.cs +++ b/Socialvoid/Security/Otp/Base32Encoding.cs @@ -45,7 +45,7 @@ namespace Socialvoid.Security.Otp /// public static byte[] ToBytes(string input) { - if(string.IsNullOrEmpty(input)) + if (string.IsNullOrEmpty(input)) { throw new ArgumentNullException("input"); } @@ -61,7 +61,7 @@ namespace Socialvoid.Security.Otp { int cValue = CharToValue(c); - if(bitsRemaining > 5) + if (bitsRemaining > 5) { mask = cValue << (bitsRemaining - 5); curByte = (byte)(curByte | mask); @@ -77,8 +77,8 @@ namespace Socialvoid.Security.Otp } } - //if we didn't end with a full byte - if(arrayIndex != byteCount) + // in the case we didn't end with a full byte + if (arrayIndex != byteCount) { returnArray[arrayIndex] = curByte; } @@ -93,7 +93,7 @@ namespace Socialvoid.Security.Otp /// public static string ToString(byte[] input) { - if(input == null || input.Length == 0) + if (input == null || input.Length == 0) { throw new ArgumentNullException("input"); } @@ -109,7 +109,7 @@ namespace Socialvoid.Security.Otp nextChar = (byte)(nextChar | (b >> (8 - bitsRemaining))); returnArray[arrayIndex++] = ValueToChar(nextChar); - if(bitsRemaining < 4) + if (bitsRemaining < 4) { nextChar = (byte)((b >> (3 - bitsRemaining)) & 31); returnArray[arrayIndex++] = ValueToChar(nextChar); @@ -120,8 +120,8 @@ namespace Socialvoid.Security.Otp nextChar = (byte)((b << bitsRemaining) & 31); } - //if we didn't end with a full char - if(arrayIndex != charCount) + // in the case we didn't end with a full char + if (arrayIndex != charCount) { returnArray[arrayIndex++] = ValueToChar(nextChar); while(arrayIndex != charCount) returnArray[arrayIndex++] = '='; //padding @@ -137,19 +137,19 @@ namespace Socialvoid.Security.Otp private static int CharToValue(char c) { // 65 - 90 == uppercase letters - if(c < 91 && c > 64) + if (c < 91 && c > 64) { return c - 65; } // 50 - 55 == numbers 2-7 - if(c < 56 && c > 49) + if (c < 56 && c > 49) { return c - 24; } // 97 - 122 == lowercase letters - if(c < 123 && c > 96) + if (c < 123 && c > 96) { return c - 97; } diff --git a/Socialvoid/Security/Otp/Hotp.cs b/Socialvoid/Security/Otp/Hotp.cs index 282b50a..31200ea 100644 --- a/Socialvoid/Security/Otp/Hotp.cs +++ b/Socialvoid/Security/Otp/Hotp.cs @@ -59,7 +59,7 @@ namespace Socialvoid.Security.Otp public Hotp(byte[] secretKey, OtpHashMode mode = OtpHashMode.Sha1, int hotpSize = 6) : base(secretKey, mode) { - if(hotpSize < 6 || hotpSize > 8) + if (hotpSize < 6 || hotpSize > 8) { throw new ArgumentOutOfRangeException(nameof(hotpSize), "The hotpSize must be between 6 and 8"); @@ -87,7 +87,7 @@ namespace Socialvoid.Security.Otp : base(key, mode) { - if(hotpSize < 6 || hotpSize > 8) + if (hotpSize < 6 || hotpSize > 8) { throw new ArgumentOutOfRangeException(nameof(hotpSize), "The hotpSize must be between 6 and 8"); diff --git a/Socialvoid/Security/Otp/InMemoryKey.cs b/Socialvoid/Security/Otp/InMemoryKey.cs index 9ed378d..81a62ce 100644 --- a/Socialvoid/Security/Otp/InMemoryKey.cs +++ b/Socialvoid/Security/Otp/InMemoryKey.cs @@ -79,10 +79,12 @@ namespace Socialvoid.Security.Otp /// Plaintext key data public InMemoryKey(byte[] key) { - if(!(key != null)) - throw new ArgumentNullException("key"); - if(!(key.Length > 0)) - throw new ArgumentException("The key must not be empty"); + if (key == null || key.Length == 0) + { + throw new ArgumentException("Key cannot be empty or null", + nameof(key)); + } + _keyLength = key.Length; int paddedKeyLength = (int)Math.Ceiling((decimal)key.Length / (decimal)16) * 16; diff --git a/Socialvoid/Security/SessionIdentification.cs b/Socialvoid/Security/SessionIdentification.cs index 6524d6e..48ae07e 100644 --- a/Socialvoid/Security/SessionIdentification.cs +++ b/Socialvoid/Security/SessionIdentification.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace Socialvoid.Security { /// @@ -26,17 +28,20 @@ namespace Socialvoid.Security /// The ID of the session obtained when establishing a session. /// since: v0.0.0 /// + [JsonPropertyName("session_id")] public string SessionID { get; internal set; } /// /// The Public Hash of the client used when establishing the session. /// since: v0.0.0 /// + [JsonPropertyName("client_public_hash")] public string ClientPublicHash { get; internal set; } /// /// The session challenge answer revolving around the client's /// private hash, the same client used to establish the session. /// since: v0.0.0 /// + [JsonPropertyName("challenge_answer")] public string ChallengeAnswer { get; internal set; } #endregion //-------------------------------------------------