public void TestCreateHashSha512()
        {
            CryptoHelper crypto = new CryptoHelper();
            string pwAsUserEnteredString = "The Sun Did Not Shine";
            byte[] result = crypto.CreateHashSha512(pwAsUserEnteredString);
            string resultstr = BitConverter.ToString(result);

            Assert.AreEqual("1", "1");
        }
        public void TestIsPasswordCorrectForTrue()
        {
            CryptoHelper crypto = new CryptoHelper();
            string pw = "The Sun Did Not Shine";
            string pwAsUserEnteredString = "The Sun Did Not Shine";

            byte[] pwAsByteArrayFromDB = crypto.CreateHashSha512(pw);

            bool pwcorrect = crypto.IsPasswordCorrect(pwAsUserEnteredString, pwAsByteArrayFromDB);

            Assert.IsTrue(pwcorrect);
        }
        public void TestIsPasswordCorrectForFalse()
        {
            CryptoHelper crypto = new CryptoHelper();
            string pw = "The Sun Did Not Shine";
            string pwAsUserEnteredString = "It Was Too Wet To Play";
            //string pwAsUserEnteredString = "the sun did not shine";

            byte[] pwAsByteArrayFromDB = crypto.CreateHashSha512(pw);

            bool pwcorrect = crypto.IsPasswordCorrect(pwAsUserEnteredString, pwAsByteArrayFromDB);

            Assert.IsFalse(pwcorrect);
        }
Beispiel #4
0
        public static string GetSecureCookieValue(string cookieName)
        {
            if (HttpContext.Current == null) return String.Empty;
            if (String.IsNullOrEmpty(cookieName)) return String.Empty;
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookieName);
            if (cookie == null) return string.Empty;

            CryptoHelper cryptoHelper = new CryptoHelper();

            string value = DecryptAndVerifyCookie(
                cryptoHelper,
                cookie,
                HttpContext.Current.Request.ServerVariables);

            return value.ToString();
        }
Beispiel #5
0
        public static string DecryptAndVerifyCookie(
            CryptoHelper cryptoHelper,
            HttpCookie cookie,
            NameValueCollection serverVariables)
        {
            if (cookie == null) return null;

            string[] values;

            if (!cryptoHelper.DecryptAndVerifyData(cookie.Value, out values))
                return null;

            if (values.Length == 3) // 3 values, has an expiry date
            {
                DateTime expireDate = DateTime.Parse(values[2]);
                if (expireDate < DateTime.Now)
                    return null;
            }

            if (values[1] != serverVariables["REMOTE_ADDR"])
                return null;

            return values[0];
        }
Beispiel #6
0
        public static void SignAndSecureCookie(
            CryptoHelper cryptoHelper,
            HttpCookie cookie,
            NameValueCollection
            serverVariables)
        {
            if (cookie.HasKeys)
                throw (new Exception("Does not support cookies with sub keys"));

            if (cookie.Expires != DateTime.MinValue) // has an expiry date
            {
                cookie.Value = cryptoHelper.SignAndSecureData(new string[]
                    {
                        cookie.Value,
                        serverVariables["REMOTE_ADDR"],
                        cookie.Expires.ToString()});
            }
            else
            {
                cookie.Value = cryptoHelper.SignAndSecureData(
                    new string[] { cookie.Value, serverVariables["REMOTE_ADDR"] });
            }
        }
Beispiel #7
0
        public static void SetSecureCookie(String cookieName, String cookieValue)
        {
            if (String.IsNullOrEmpty(cookieName) || String.IsNullOrEmpty(cookieValue)) return;
            if (HttpContext.Current == null) return;
            HttpCookie cookie = new HttpCookie(cookieName, cookieValue);
            cookie.HttpOnly = true;
            //cookie.Expires = DateTime.Now.AddYears(1);
            CryptoHelper cryptoHelper = new CryptoHelper();
            SignAndSecureCookie(
                    cryptoHelper,
                    cookie,
                    HttpContext.Current.Request.ServerVariables);

            HttpContext.Current.Response.Cookies.Add(cookie);
        }
Beispiel #8
0
 /// <summary>
 /// Computes and returns the 16-byte MD5 hash of the GUID.
 /// </summary>
 /// <param name="guid">The GUID.</param>
 /// <returns>The 16-byte hash.</returns>
 public static byte[] ToMd5Bytes(this Guid guid)
 {
     return(CryptoHelper.ComputeMD5Bytes(guid.ToByteArray()));
 }