public void Crypt() { string dataToCrypt = CryptoRandomizer.GenerateSecurityKey(16).ToUrlSafeBase64(); rsaCypherMock = new Mock <IRsaCypher>(); rsaCypherMock.Setup(x => x.GenerateKeys()).Returns <(string, string)>(result => result); var keys = rsaCypherMock.Object.GenerateKeys(); string cryptedText = rsaCypherMock.Object.Crypt(keys.publicKey, dataToCrypt); string decryptedText = rsaCypherMock.Object.Decrypt(keys.privateKey, cryptedText); Assert.Equal(decryptedText, dataToCrypt); }
public string GenerateAesKey() { var result = CryptoRandomizer.GenerateSecurityKey(32); return(result.ToUrlSafeBase64()); }
public async Task <CreateFirstMessangerSessionResponse> MakeFirstSessionAsync(CreateMessangerSessionRequest model, string userId, string sessionId) { var savedSessions = await sessionProvider.GetModelBySearchPredicate(x => x.SessionId == sessionId && x.UserId == userId); if (savedSessions != null) { throw new ApiError(new ServerException("This session is alredy exist")); } var serverKeys = rsaCypher.GenerateKeys(); var user = await userManager.Users.FirstOrDefaultAsync(x => x.Id == userId); var serverSession = new Session() { ClientPublicKey = model.PublicKey, ServerPrivateKey = serverKeys.privateKey, ServerPublicKey = serverKeys.publicKey, SessionId = sessionId, UserId = userId }; var savedStrongKey = await strongKeyProvider.GetModelBySearchPredicate(x => x.UserId == userId); if (savedStrongKey != null) { var savedCypher = await cypherProvider.GetModelBySearchPredicate(x => x.Id == savedStrongKey.CypherId); await sessionProvider.CreateOrUpdateAsync(serverSession); return(new CreateFirstMessangerSessionResponse() { ServerPublicKey = serverKeys.publicKey, CryptedAes = rsaCypher.Crypt(model.PublicKey, savedCypher.Secret.ToUrlSafeBase64()) }); } else { await using (var transaction = await transactionProvider.BeginTransactionAsync()) { try { await sessionProvider.CreateOrUpdateAsync(serverSession); var savedRole = await roleManager.Roles.FirstOrDefaultAsync(x => x.Name == "ProtocoledUsers"); await userManager.AddToRoleAsync(user, savedRole.Name); byte[] strongKey = CryptoRandomizer.GenerateSecurityKey(16); var cypher = await cypherProvider.CreateOrUpdateAsync(new Cypher() { Secret = strongKey }); var key = await strongKeyProvider.CreateOrUpdateAsync(new StrongKey() { CypherId = cypher.Id, UserId = user.Id }); string strongKeyToCrypt = strongKey.ToUrlSafeBase64(); string cryptedAesKey = rsaCypher.Crypt(model.PublicKey, strongKeyToCrypt); await transaction.CommitAsync(); return(new CreateFirstMessangerSessionResponse() { ServerPublicKey = serverKeys.publicKey, CryptedAes = cryptedAesKey }); } catch (Exception ex) { await transaction.RollbackAsync(); throw new ApiError(new ServerException(ex.Message)); } } } }
protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity <LongSession>().HasKey(x => x.Id); modelBuilder.Entity <Cypher>().HasKey(x => x.Id); modelBuilder.Entity <StrongKey>().HasKey(x => x.Id); modelBuilder.Entity <Picture>().HasKey(x => x.Id); modelBuilder.Entity <Message>().HasKey(x => x.Id); modelBuilder.Entity <PushTask>().HasKey(x => x.Id); modelBuilder.Entity <City>().HasKey(x => x.Id); modelBuilder.Entity <Country>().HasKey(x => x.Id); modelBuilder.Entity <Like>().HasKey(x => x.Id); modelBuilder.Entity <Profile>().HasKey(x => x.Id); modelBuilder.Entity <Visit>().HasKey(x => x.Id); modelBuilder.Entity <UserPair>().HasKey(x => x.Id); modelBuilder.Entity <Cordinate>().HasKey(x => x.Id); modelBuilder.Entity <RsaKey>().HasKey(x => x.Id); modelBuilder.Entity <UserAccount>().HasKey(x => x.Id); modelBuilder.Entity <UserToken>().HasKey(x => x.Id); modelBuilder.Entity <Session>().HasKey(x => x.Id); modelBuilder.Entity <Connection>().HasKey(x => x.Id); modelBuilder.Entity <Dialog>().HasKey(x => x.Id); modelBuilder.Entity <AccountingComment>().HasKey(x => x.Id); modelBuilder.Entity <Flow>().HasKey(x => x.Id); modelBuilder.Entity <AccountingPlan>().HasKey(x => x.Id); modelBuilder.Entity <AccountingRecord>().HasKey(x => x.Id); modelBuilder.Entity <LongSession>() .HasOne(x => x.User) .WithMany(x => x.LongSessions); modelBuilder.Entity <Message>() .HasOne(x => x.Cypher); modelBuilder.Entity <PushTask>() .HasOne(x => x.User) .WithMany(x => x.PushTasks); modelBuilder.Entity <City>() .HasOne(x => x.Country) .WithMany(x => x.Cities); modelBuilder.Entity <Like>() .HasOne(x => x.User) .WithMany(x => x.Likes); modelBuilder.Entity <Visit>() .HasOne(x => x.User) .WithMany(x => x.Visits); modelBuilder.Entity <Picture>() .HasOne(x => x.User) .WithMany(x => x.Pictures); modelBuilder.Entity <Message>() .HasOne(x => x.Cypher); modelBuilder.Entity <Profile>() .HasMany(x => x.Characters) .WithOne(x => x.Profile); modelBuilder.Entity <Profile>() .HasOne(x => x.User) .WithOne(x => x.Profile) .HasForeignKey <Profile>(x => x.UserId); modelBuilder.Entity <Profile>() .HasOne(x => x.Country) .WithMany(x => x.Profiles); modelBuilder.Entity <City>() .HasMany(x => x.Profiles) .WithOne(x => x.City); modelBuilder.Entity <Cordinate>() .HasOne(x => x.User) .WithMany(x => x.Cordinates); modelBuilder.Entity <RsaKey>() .HasOne(x => x.User) .WithMany(x => x.RsaKeys); modelBuilder.Entity <User>() .HasMany(x => x.UserTokens) .WithOne(x => x.User); modelBuilder.Entity <User>() .HasMany(x => x.Sessions) .WithOne(x => x.User); modelBuilder.Entity <User>() .HasMany(x => x.Sessions) .WithOne(x => x.User); modelBuilder.Entity <StrongKey>() .HasOne(x => x.Cypher); modelBuilder.Entity <StrongKey>() .HasOne(x => x.User) .WithOne(x => x.StrongKey) .HasForeignKey <StrongKey>(x => x.UserId); modelBuilder.Entity <AccountingRecord>() .HasOne(x => x.User) .WithMany(x => x.AccountingRecords); modelBuilder.Entity <LongSession>().HasIndex(x => x.UserId); modelBuilder.Entity <Message>().HasIndex(x => new { x.ReceiverId, x.SenderId }); modelBuilder.Entity <PushTask>().HasIndex(x => x.UserId); modelBuilder.Entity <Like>().HasIndex(x => x.UserId); modelBuilder.Entity <Visit>().HasIndex(x => x.UserId); modelBuilder.Entity <Profile>().HasIndex(x => x.UserId); modelBuilder.Entity <UserPair>().HasIndex(x => new { x.UserId1, x.UserId2 }); modelBuilder.Entity <Message>().HasIndex(x => x.MessageId); modelBuilder.Entity <IdentityRole>().HasData(new IdentityRole[] { new IdentityRole() { Name = "User", NormalizedName = "USER" }, new IdentityRole() { Name = "Admin", NormalizedName = "ADMIN" }, new IdentityRole() { Name = "ProtocoledUsers", NormalizedName = "PROTOCOLEDUSERS" } }); modelBuilder.Entity <Cypher>().HasData(new Cypher[] { new Cypher() { Id = 1, Secret = CryptoRandomizer.GenerateSecurityKey(32) } }); modelBuilder.Entity <UserAccount>().HasData(new UserAccount[] { new UserAccount() { Id = "1", Login = "******", Password = "******" }, new UserAccount() { Id = "2", Login = "******", Password = "******" } }); modelBuilder.Entity <AccountingPlan>().HasData(AccountingInitializers.GetAccountingsPlan()); }