1 static void Main( string[] args ) 2 { 3 string s = getTimestamp(); 4 string ss = getNoncestr(); 5 Console.WriteLine( ss ); 6 Console.WriteLine( s ); 7 Console.ReadKey(); 8 }
/* * / <summary> * / 生成時間戳 * / 從 1970 年 1 月 1 日 00:00:00 至今的秒數,即當前的時間,且最終需要轉換為字符串形式 * / </summary> * / <returns></returns> */ public string getTimestamp() { TimeSpan ts = DateTime.UtcNow - new DateTime( 1970, 1, 1, 0, 0, 0, 0 ); return(Convert.ToInt64( ts.TotalSeconds ).ToString() ); }
public static string getNoncestr() { Random random = new Random(); return(MD5Util.GetMD5( random.Next( 1000 ).ToString(), "GBK" ) ); } public class MD5Util { public MD5Util() { /* * * TODO: 在此處添加構造函數邏輯 * */ } /** 獲取大寫的MD5簽名結果 */ public static string GetMD5( string encypStr, string charset ) { string retStr; MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider(); /* 創建md5對象 */ byte[] inputBye; byte[] outputBye; /* 使用GB2312編碼方式把字符串轉化為字節數組. */ try { inputBye = Encoding.GetEncoding( charset ).GetBytes( encypStr ); } catch ( Exception ex ) { inputBye = Encoding.GetEncoding( "GB2312" ).GetBytes( encypStr ); } outputBye = m5.ComputeHash( inputBye ); retStr = System.BitConverter.ToString( outputBye ); retStr = retStr.Replace( "-", "" ).ToUpper(); return(retStr); } }
