Quellcode durchsuchen

Added Authenticator Code to InABox.Core
Standardised location for Digital Key BT Identifiers

frankvandenbos vor 5 Monaten
Ursprung
Commit
b183e31167

+ 2 - 2
InABox.Avalonia.Platform.Desktop/InABox.Avalonia.Platform.Desktop.csproj

@@ -13,8 +13,8 @@
     </ItemGroup>
 
     <ItemGroup>
-      <PackageReference Include="PDFtoImage" />
-      <PackageReference Include="SkiaSharp" />
+      <PackageReference Include="PDFtoImage" Version="5.0.0" />
+      <PackageReference Include="SkiaSharp" Version="3.116.1" />
     </ItemGroup>
 
 </Project>

+ 1 - 0
InABox.Avalonia.Platform.iOS/InABox.Avalonia.Platform.iOS.csproj

@@ -25,6 +25,7 @@
       <PackageReference Include="bblanchon.PDFium.iOS" Version="135.0.7009" />
       <PackageReference Include="Microsoft.Maui.Essentials" Version="9.0.40" />
       <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
+      <PackageReference Include="SkiaSharp" Version="3.116.1" />
     </ItemGroup>
     
 </Project>

+ 5 - 0
InABox.Avalonia.Platform/PlatformTools.cs

@@ -50,6 +50,11 @@ public static class PlatformTools
         }
     }
     
+    public static Guid DigitalKeyServiceId = Guid.Parse("ce6c0b18-0000-1000-8000-00805F9B34FB");
+    public static Guid DigitalKeyConfigId = Guid.Parse("447c1982-77ef-49be-a39a-2920f33c31e5");
+    public static Guid DigitalKeyControlId = Guid.Parse("5b804487-b73f-406a-8240-649c23ad1590");
+    public static double ScanTimeoutInSeconds = 15;
+    
     private static IBluetooth? _bluetooth;
     public static IBluetooth Bluetooth
     {

+ 1 - 0
InABox.Avalonia/InABox.Avalonia.csproj

@@ -27,6 +27,7 @@
       <PackageReference Include="Microsoft.Maui.Essentials" Version="9.0.40" />
       <PackageReference Include="Serilog" Version="4.2.0" />
       <PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
+      <PackageReference Include="SkiaSharp" Version="3.116.1" />
       <PackageReference Include="Syncfusion.Licensing" Version="26.2.14" />
       <PackageReference Include="Syncfusion.Pdf.Net.Core" Version="26.2.14" />
     </ItemGroup>

+ 82 - 0
InABox.Core/Authenticator.cs

@@ -0,0 +1,82 @@
+using System;
+using System.Linq;
+using System.Security.Cryptography;
+
+namespace InABox.Core
+{
+    public static class Authenticator
+    {
+        
+        public static readonly int CODE_LENGTH = 6;
+        
+        private static readonly int CODE_MODULO = (int)Math.Pow(10, CODE_LENGTH);
+        
+        private static byte[] FromHexString(string hex) {
+            if (hex.Length % 2 == 1)
+                throw new Exception("The binary key cannot have an odd number of digits");
+        
+            byte[] arr = new byte[hex.Length >> 1];
+        
+            for (int i = 0; i < hex.Length >> 1; ++i)
+            {
+                arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
+            }
+        
+            return arr;
+        }
+        
+        public static int GetHexVal(char hex) {
+            int val = (int)hex;
+            //For uppercase A-F letters:
+            //return val - (val < 58 ? 48 : 55);
+            //For lowercase a-f letters:
+            //return val - (val < 58 ? 48 : 87);
+            //Or the two combined, but a bit slower:
+            return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
+        }
+
+        public static string GenerateGoogleAuthenticatorCode(byte[] key)
+        {
+            var _time = DateTimeOffset.Now.ToUnixTimeSeconds();
+            return GenerateGoogleAuthenticatorCode(_time, key);
+        }
+        
+        private static string GenerateGoogleAuthenticatorCode(long time, byte[] key)
+        {
+            var _window = time / 30;
+            var _hex = _window.ToString("x");
+            if (_hex.Length < 16)
+            {
+                _hex = _hex.PadLeft(16, '0');
+            }
+            var _bytes = FromHexString(_hex);
+            var _hash = new HMACSHA1(key).ComputeHash(_bytes);
+
+            var _offset = _hash.Last() & 0xf;
+            var _selected = new byte[4];
+            Buffer.BlockCopy(_hash, _offset, _selected, 0, 4);
+            if (BitConverter.IsLittleEndian)
+            {
+                Array.Reverse(_selected);
+            }
+
+            var _integer = BitConverter.ToInt32(_selected, 0);
+            var _truncated = _integer & 0x7fffffff;
+
+            return (_truncated % CODE_MODULO).ToString().PadLeft(CODE_LENGTH, '0');
+        }
+
+        public static bool CheckAuthenticationCode(byte[] key, string code)
+        {
+            var _time = DateTimeOffset.Now.ToUnixTimeSeconds();
+            for (long _l = _time - 30; _l <= _time; _l += 30)
+            {
+                if(GenerateGoogleAuthenticatorCode(_l, key) == code)
+                {
+                    return true;
+                }
+            }
+            return false;
+        }
+    }
+}