Monday, July 6, 2015

Generate Random numbers using RNGCryptoServiceProvider in C#

Random numbers used for creating encryption keys and for hashing. Good random numbers are important in Cryptography as it deals with security. The .NET Framework has two ways to generating random values.
  1. System.Random 
  2. System.Security.Cryptography.RNGCryptoServiceProvider
System.Random:
System.Random is not good enough for cryptographic purposes. Microsoft recommends creating one instance of System.Random to generate numbers for your application. Also it's not thread safe.

System.Security.Cryptography.RNGCryptoServiceProvider:
The RNGCryptoServiceProvider is used as cryptographic random number generators as it can not be predict. It is a more secure way to generate random numbers.

The following Console application sample generates 10 random values.
using System;
using System.Security.Cryptography;
static void Main()
{
    for (var i = 0; i < 10; i++)
    {
        int keySize = 32;
        var randomValueByte = GenerateRandomValue(keySize);
        Console.WriteLine(Convert.ToBase64String(randomValueByte));
    }

    Console.ReadLine();
}

public static byte[] GenerateRandomValue(int keySize )
{
    var randomValue = new byte[keySize];
    using (var randomValueGenerator = new RNGCryptoServiceProvider())
    {
        randomValueGenerator.GetBytes(randomValue);
    }
    return randomValue;
}
Output:
Check my another article to learn Cryptographic Hashing Algorithm in .Net.

Wednesday, July 1, 2015

Get invalid fileds/components list from ExtJS form when the form.isValid() returns false

To list the invalid fileds/components from ExtJS form when the form is inValid (form.isValid() == false), use the below script,
var formPanel = this.down('form');
var form = formPanel.getForm();
var invalidFields = form.query("field{isValid()==false}");
So the variable invalidFields will hold the invalid fileds/components list.

(Solved) SQL Server Error: The subscription(s) have been marked inactive and must be reinitialized. NoSync subscriptions will need to be dropped and recreated.

Solution:

1. Run the below query to check the status of the subscription
use distribution
go
select * From distribution..MSsubscriptions
2. Note down the publisher_id, publisher_db, publication_id, subscriber_id, subscriber_db if the status is 0

3. Then update the status by runing the below query, apply the where condition value properly that you have noted in the previous step.
use distribution
go
if exists (select * from distribution..MSsubscriptions where status = 0)
begin
    UPDATE distribution..MSsubscriptions
    SET STATUS = 2
    WHERE publisher_id = 0
    AND publisher_db = 'Publisher_Db'
    AND publication_id = 16
    AND subscriber_id = 0
    AND subscriber_db ='Subscriber_Db'
end
 else
begin
 print 'All the subscription is Active'
end
4. Check the replication monitor for any issues.