mirror of
https://github.com/bitwarden/directory-connector
synced 2025-12-17 16:53:25 +00:00
move crypto to a util. create sync util
This commit is contained in:
@@ -59,16 +59,15 @@
|
|||||||
<Compile Include="Models\TokenResponse.cs" />
|
<Compile Include="Models\TokenResponse.cs" />
|
||||||
<Compile Include="Services\ApiService.cs" />
|
<Compile Include="Services\ApiService.cs" />
|
||||||
<Compile Include="Services\SettingsService.cs" />
|
<Compile Include="Services\SettingsService.cs" />
|
||||||
<Compile Include="Services\CryptoService.cs" />
|
<Compile Include="Utilities\Crypto.cs" />
|
||||||
<Compile Include="Services\TokenService.cs" />
|
<Compile Include="Services\TokenService.cs" />
|
||||||
<Compile Include="Services\AuthService.cs" />
|
<Compile Include="Services\AuthService.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="Utilities\Sync.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup />
|
||||||
<Folder Include="Utilities\" />
|
|
||||||
</ItemGroup>
|
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using Bit.Core.Models;
|
using Bit.Core.Models;
|
||||||
|
using Bit.Core.Utilities;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -38,12 +39,12 @@ namespace Bit.Core.Services
|
|||||||
public async Task<LoginResult> LogInAsync(string email, string masterPassword)
|
public async Task<LoginResult> LogInAsync(string email, string masterPassword)
|
||||||
{
|
{
|
||||||
var normalizedEmail = email.Trim().ToLower();
|
var normalizedEmail = email.Trim().ToLower();
|
||||||
var key = CryptoService.Instance.MakeKeyFromPassword(masterPassword, normalizedEmail);
|
var key = Crypto.MakeKeyFromPassword(masterPassword, normalizedEmail);
|
||||||
|
|
||||||
var request = new TokenRequest
|
var request = new TokenRequest
|
||||||
{
|
{
|
||||||
Email = normalizedEmail,
|
Email = normalizedEmail,
|
||||||
MasterPasswordHash = CryptoService.Instance.HashPasswordBase64(key, masterPassword)
|
MasterPasswordHash = Crypto.HashPasswordBase64(key, masterPassword)
|
||||||
};
|
};
|
||||||
|
|
||||||
var response = await ApiService.Instance.PostTokenAsync(request);
|
var response = await ApiService.Instance.PostTokenAsync(request);
|
||||||
@@ -74,10 +75,9 @@ namespace Bit.Core.Services
|
|||||||
public async Task<LoginResult> LogInTwoFactorAsync(string token, string email, string masterPassword)
|
public async Task<LoginResult> LogInTwoFactorAsync(string token, string email, string masterPassword)
|
||||||
{
|
{
|
||||||
var normalizedEmail = email.Trim().ToLower();
|
var normalizedEmail = email.Trim().ToLower();
|
||||||
var key = CryptoService.Instance.MakeKeyFromPassword(masterPassword, normalizedEmail);
|
var key = Crypto.MakeKeyFromPassword(masterPassword, normalizedEmail);
|
||||||
|
|
||||||
var result = await LogInTwoFactorWithHashAsync(token, email,
|
var result = await LogInTwoFactorWithHashAsync(token, email, Crypto.HashPasswordBase64(key, masterPassword));
|
||||||
CryptoService.Instance.HashPasswordBase64(key, masterPassword));
|
|
||||||
|
|
||||||
key = null;
|
key = null;
|
||||||
masterPassword = null;
|
masterPassword = null;
|
||||||
|
|||||||
@@ -2,34 +2,13 @@
|
|||||||
using Org.BouncyCastle.Crypto.Generators;
|
using Org.BouncyCastle.Crypto.Generators;
|
||||||
using Org.BouncyCastle.Crypto.Parameters;
|
using Org.BouncyCastle.Crypto.Parameters;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Security.Cryptography;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Bit.Core.Services
|
namespace Bit.Core.Utilities
|
||||||
{
|
{
|
||||||
public class CryptoService
|
public static class Crypto
|
||||||
{
|
{
|
||||||
private static CryptoService _instance;
|
public static byte[] MakeKeyFromPassword(string password, string salt)
|
||||||
|
|
||||||
private CryptoService() { }
|
|
||||||
|
|
||||||
public static CryptoService Instance
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
if(_instance == null)
|
|
||||||
{
|
|
||||||
_instance = new CryptoService();
|
|
||||||
}
|
|
||||||
|
|
||||||
return _instance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] MakeKeyFromPassword(string password, string salt)
|
|
||||||
{
|
{
|
||||||
if(password == null)
|
if(password == null)
|
||||||
{
|
{
|
||||||
@@ -52,14 +31,14 @@ namespace Bit.Core.Services
|
|||||||
return keyBytes;
|
return keyBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string MakeKeyFromPasswordBase64(string password, string salt)
|
public static string MakeKeyFromPasswordBase64(string password, string salt)
|
||||||
{
|
{
|
||||||
var key = MakeKeyFromPassword(password, salt);
|
var key = MakeKeyFromPassword(password, salt);
|
||||||
password = null;
|
password = null;
|
||||||
return Convert.ToBase64String(key);
|
return Convert.ToBase64String(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] HashPassword(byte[] key, string password)
|
public static byte[] HashPassword(byte[] key, string password)
|
||||||
{
|
{
|
||||||
if(key == null)
|
if(key == null)
|
||||||
{
|
{
|
||||||
@@ -81,7 +60,7 @@ namespace Bit.Core.Services
|
|||||||
return hashBytes;
|
return hashBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string HashPasswordBase64(byte[] key, string password)
|
public static string HashPasswordBase64(byte[] key, string password)
|
||||||
{
|
{
|
||||||
var hash = HashPassword(key, password);
|
var hash = HashPassword(key, password);
|
||||||
password = null;
|
password = null;
|
||||||
@@ -89,7 +68,7 @@ namespace Bit.Core.Services
|
|||||||
return Convert.ToBase64String(hash);
|
return Convert.ToBase64String(hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] DeriveKey(byte[] password, byte[] salt, int rounds)
|
private static byte[] DeriveKey(byte[] password, byte[] salt, int rounds)
|
||||||
{
|
{
|
||||||
var generator = new Pkcs5S2ParametersGenerator(new Sha256Digest());
|
var generator = new Pkcs5S2ParametersGenerator(new Sha256Digest());
|
||||||
generator.Init(password, salt, rounds);
|
generator.Init(password, salt, rounds);
|
||||||
12
src/Core/Utilities/Sync.cs
Normal file
12
src/Core/Utilities/Sync.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Bit.Core.Utilities
|
||||||
|
{
|
||||||
|
public static class Sync
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user