Strong Password Generator in C#

This code snippets will let you auto-generate strong passwords. Look for the password rules in the inline comments in code

001 using System;
002 using System.Collections.Generic;
003 using System.Linq;
004 using System.Security.Cryptography.X509Certificates;
005 using System.Text;
006 using System.Threading.Tasks;
007
008 namespace PwdGen
009 {
010     class Program
011     {
012         static void Main(string[] args)
013         {
014             Console.WriteLine(GeneratePassword(815));
015             Console.ReadKey();
016         }
017
018         static string GeneratePassword(int MinLengthint MaxLength)
019         {
020             string ValidChars "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:;<>|=.,_-~!?&%@#$£€°^*§()+[] ";
021             string SpecialChars "!@#$%^&*()";
022             
023             string pwd string.Empty;
024
025             Random rnd = new Random(Guid.NewGuid().GetHashCode());
026             
027             bool NumExist false;
028             bool IsUpper false;
029             bool IsLower false;
030             bool IsSplChr false;
031             bool NoRepeat true;
032             bool NoSeq true;
033
034             while ((!NumExist || !IsUpper || !IsLower || !IsSplChr) && (NoRepeat || NoSeq) )
035             {
036                 NumExist false;
037                 IsUpper false;
038                 IsLower false;
039                 IsSplChr false;
040                 NoRepeat true;
041                 NoSeq true;
042                 
043                 pwd string.Join(string.Empty, Enumerable.Repeat(ValidCharsrnd.Next(MinLengthMaxLength 1)).Select(=> s[rnd.Next(s.Length)]).ToArray());
044                 for (int i 0pwd.Lengthi++)
045                 {
046                     // Contains at least 1 lower case letter and 1 upper case letter (all UTF-8), at least 1 number
047
048                     if (!NumExist)
049                     {
050                         NumExist = (char.IsDigit(pwd[i]) && true);
051                     }
052                     if (!IsUpper)
053                     {
054                         IsUpper = (char.IsUpper(pwd[i]) && true);
055                     }
056                     if (!IsLower)
057                     {
058                         IsLower = (char.IsLower(pwd[i]) && true);
059                     }
060
061                     // A predefined set of special chars must be present
062                     if (!IsSplChr)
063                     {
064                         IsSplChr = (SpecialChars.IndexOf(pwd[i]) >= 0);
065                     }
066
067
068                     // Not more than 2 identical characters in a row (e.g., 111 not allowed)
069                     if (pwd.Length && NoRepeat)
070                     {
071                         NoRepeat = !((pwd[i] == pwd[1]) && (pwd[i] == pwd[2]));
072                     }
073
074                     // Not any sequence of the English alphabet / numbers (above 3 letters)
075                     if (pwd.Length && NoSeq)
076                     {
077                         NoSeq = !((pwd[2] - pwd[1]) == (pwd[1] - pwd[i]));
078                     }
079                     Console.WriteLine(!NumExist +" "+ !IsUpper " " + !IsLower " " + !IsSplChr " " +  NoRepeat +NoSeq);
080                 }
081                 
082             }
083             
084             return pwd;
085
086         }
087     }
088 }
089

View in GitHub | Made with wp-showgithubfile plugin

Upload/Download file to/fro MongoDB in Java

// This code is just for my reference

 public static void main(String[] args) {
Logger mongoLogger = Logger.getLogger( "org.mongodb.driver" );
mongoLogger.setLevel(Level.SEVERE);

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("testdb");

GridFSBucket gridFSFilesBucket = GridFSBuckets.create(database);
ObjectId fileId = new ObjectId();
try {

InputStream streamToUploadFrom = new FileInputStream(new File("d:\\200mb.mkv"));

// GridFSUploadOptions options = new GridFSUploadOptions()
// .chunkSizeBytes(1000)
// .metadata(new Document("type", "presentation"));

fileId = gridFSFilesBucket.uploadFromStream("myfile", streamToUploadFrom);

System.out.println("ObjectID" + fileId);
} catch(FileNotFoundException ex) {
System.out.println("Error" + ex.getMessage());
}


try {
FileOutputStream streamToDownloadTo = new FileOutputStream("d:/out.mkv");
gridFSFilesBucket.downloadToStream(fileId , streamToDownloadTo);
streamToDownloadTo.close();
System.out.println("Finished!");
} catch (IOException e) {
// handle exception
}
}

Bookmark: Raise event from a WPF User control

This blog is just for sample code keeping.

// UserControl1.xaml

<UserControl x:Class=”WpfApp1.UserControl1″

             xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

             xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

             xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″

             xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″

             xmlns:local=”clr-namespace:WpfApp1″

             mc:Ignorable=”d” Background=”Red” Height=”186.646″ Width=”411.693″>

    <Grid>

        <Button Content=”Button” HorizontalAlignment=”Left” Margin=”80,72,0,0″ VerticalAlignment=”Top” Width=”75″ Click=”Button_Click”/>

    </Grid>

</UserControl>


// UserControl1.xaml.cs

namespace WpfApp1

{

    /// <summary>

    /// Interaction logic for UserControl1.xaml

    /// </summary>

    public partial class UserControl1 : UserControl

    {

        public event EventHandler MyButtonClick;

        public UserControl1()

        {

            InitializeComponent();

        }

        private void Button_Click(object sender, RoutedEventArgs e)

        {

            this.MyButtonClick(this, new EventArgs());

        }

    }

}


// UserControl2.xaml

<UserControl x:Class=”WpfApp1.UserControl2″

             xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

             xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

             xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″

             xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″

             xmlns:local=”clr-namespace:WpfApp1″

             mc:Ignorable=”d”

             d:DesignHeight=”450″ d:DesignWidth=”800″ Background=”Green”>

    <Grid>

    </Grid>

</UserControl>

// MainWindow.xaml

<Window x:Class=”WpfApp1.MainWindow”

        xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

        xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

        xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″

        xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″

        xmlns:local=”clr-namespace:WpfApp1″

        mc:Ignorable=”d”

        Title=”MainWindow” Height=”450″ Width=”800″>

    <Grid x:Name=”MyGrid”>

        <local:UserControl1 x:Name=”userControl1″ HorizontalAlignment=”Left” Height=”100″ Margin=”99,62,0,0″ VerticalAlignment=”Top” Width=”242″ MyButtonClick=”UserControl1_MyButtonClick” />

    </Grid>

</Window>

// MainWindow.xaml.cs

namespace WpfApp1

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        private void UserControl1_MyButtonClick(object sender, EventArgs e)

        {

        

        }

    }

}

“Units” in Cloud Platforms and Services

When it comes to costing/pricing on cloud based platforms such as Azure or AWS, it is always a confusion especially for beginners what various units mean. Let us have a look at some such common unit terminologies.

  1. SKU – Stock Keeping Unit – A purchasable units in a platform. Ref – https://en.wikipedia.org/wiki/Stock_keeping_unit

  2. ACU – Azure Compute Unit – A unit used to compare compute performance across Azure SKUs. Ref – https://docs.microsoft.com/en-us/azure/virtual-machines/acu
  3. TU – Transaction Unit – Usually 10K transactions = 1 Transaction Unit
  4. DTU – Database Transaction Unit – Ref – https://docs.microsoft.com/en-us/azure/azure-sql/database/purchasing-models#understanding-dtus
  5. eDTU – elastic DTU – Ref – https://docs.microsoft.com/en-us/azure/azure-sql/database/purchasing-models#dtu-based-purchasing-model
  6. RU – Request Unit – Ref – https://docs.microsoft.com/en-us/azure/cosmos-db/request-units
  7. DBCU – Databricks Commit Unit – Ref – https://azure.microsoft.com/en-in/pricing/details/databricks/
  8. DBU – Databricks Unit – Ref – https://docs.databricks.com/administration-guide/capacity-planning/cmbp.html
  9. DPU – Database Processing Unit – Ref – https://aws.amazon.com/glue/pricing/

This list will be updated regularly.