25 Şubat 2011 Cuma

MD5 string encrypt and verify

String'i MD5 ile şifreleyip doğrulayan methodlar...

public static string MD5String(this string value)
{
  MD5 md5Hasher =MD5.Create();
  byte[] data= md5Hasher.ComputeHash(Encoding.Default.GetBytes(value));
  
  StringBuilder sBuilder = new StringBuilder();
  for(int i=0; i < data.Length; i++)
  {
     sBuilder.Append(data[i].ToString("x2"));
  }
  return sBuilder.ToString();
}

public static bool VerifyMD5String(this string value)
{
  string hashOfInput = MD5String(value);
  StringComparer comparer = StringComparer.OrdinalIgnoreCase;
  return 0== comparer.Compare(hashOfInput,hash) ? true : false;
}

Base64 String Encode and Decode Methods

Mutlaka bir yerlerde işimize yarayacağını düşündüm güzel bir method örneği;

public static string Base64StringEncode(this string value)
{
   byte[] toEncodeAsBytes= ASCIIEncoding.ASCII.GetBytes(value);
   string returnValue= Convert.ToBase64String(toEncodeAsBytes);
   return returnValue;
}

public static string Base64StringDecode(this string value)
{
   byte[] encodedDataAsBytes=Convert.FromBase64String(value);
   string returnValue= ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
   return returnValue;
}

19 Şubat 2011 Cumartesi

Port Scanner Class

Güzel bir örnek oldu basit ve hızlı..

    public class PortScanner
    {
        public List<int> ActivePorts = new List<int>();
        public List<int> InactiveReports = new List<int>();

        public void Scan(string IP, int startPort, int endPort)
        {
            Socket Sock = null;

            for (int Port = startPort; Port <= endPort; Port++)
            {
                try
                {
                    Sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    Sock.Connect(IP, Port);

                    ActivePorts.Add(Port);
                }
                catch (SocketException ex)
                {
                    InactiveReports.Add(Port);
                }
                finally
                {
                    if (Sock!=null)
                    {
                        if (Sock.Connected)
                            Sock.Disconnect(false);
                        Sock.Close();
                    }
                }
            }
        }
    }

16 Şubat 2011 Çarşamba

Converting URLs Into Links With Regex


public static string WithActiveLinks(this string s)
{
  //Finds URLs with no protocol
  var urlregex = 
new Regex(@"\b\({0,1}(?<url>(www|ftp)\.[^ ,""\s<)]*)\b", 
    RegexOptions.IgnoreCase | RegexOptions.Compiled);
  //Finds URLs with a protocol
  var httpurlregex = 
new Regex(@"\b\({0,1}(?<url>[^>]
(http://www\.|http://|https://|ftp://)[^,""\s<)]*)\b", 
    RegexOptions.IgnoreCase | RegexOptions.Compiled);
  //Finds email addresses
  var emailregex = 
new Regex(@"\b(?<mail>[a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)\b", 
    RegexOptions.IgnoreCase | RegexOptions.Compiled);
  s = urlregex.Replace
(s, " <a href=\"http://${url}\" target=\"_blank\">${url}</a>");
  s = httpurlregex.Replace
(s, " <a href=\"${url}\" target=\"_blank\">${url}</a>");
  s = emailregex.Replace
  (s, "<a href=\"mailto:${mail}\">${mail}</a>");
  return s;
}

14 Şubat 2011 Pazartesi

JavaScript Methods Samples

Dropdownlist ten dizin ya da değer alma method örneği;


function getValues(){
var dd = document.getElementById('dropdown');
theindex = dd.options[dd.selectedIndex].value;
thevalue = dd.options[dd.selectedIndex].text;
var span = document.getElementById('span1');
span.innerHTML = '  Selected Index: ' + theindex + 
 ', Selected Value: ' + thevalue
}
 
Seçilen değerde hangisi işaretli hangisi işaretsiz onu algılayan method örneği;
function getchk(val){
var output = 'You clicked ' + val + ' Selected: ';
for(var i = 0; i < document.forms[0].check.length; i++){
 if(document.forms[0].check[i].checked)
 output += document.forms[0].check[i].value + ' ';
 }
document.getElementById('span2').innerHTML = output;
}
 
Textbox a girilen yazı ve rakam değerlerini gösteren method örneği;
 
function getText(){
var txt = document.getElementById('txt');
var output = txt.value;
var length = output.length;
document.getElementById('span3').innerHTML = 'Count: ' + length
 + ' Text: ' + output;
}