Files
SkydiveLogs/Back/skydiveLogs-api.Business/UserService.cs
Sébastien André ea25a28a78 Update "User" API
2020-03-12 17:11:36 +01:00

61 lines
2.0 KiB
C#

using skydiveLogs_api.Business.Interface;
using skydiveLogs_api.Model;
using skydiveLogs_api.Data.Interface;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System;
namespace skydiveLogs_api.Business
{
public class UserService : IUserService
{
public UserService(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public User GetByLogin(string login, string password)
{
var tmp = _userRepository.GetByLogin(login, EncryptPassword(password));
return tmp;
}
public void AddNewUser(User newUser)
{
newUser.Password = EncryptPassword(newUser.Password);
_userRepository.Add(newUser);
}
private string EncryptPassword(string password)
{
var encryptionKey = "skydivelogsangular"; //we can change the code converstion key as per our requirement
byte[] clearBytes = Encoding.Unicode.GetBytes(password);
var encryptedPassword = string.Empty;
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey,
new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
encryptedPassword = Convert.ToBase64String(ms.ToArray());
}
}
return encryptedPassword;
}
private readonly IUserRepository _userRepository;
}
}