Exemplo n.º 1
0
        /// <summary>
        /// Generate a setup code for a Google Authenticator user to scan
        /// </summary>
        /// <param name="issuer">Issuer ID (the name of the system, i.e. 'MyApp'), can be omitted but not recommended https://github.com/google/google-authenticator/wiki/Key-Uri-Format </param>
        /// <param name="accountTitleNoSpaces">Account Title (no spaces)</param>
        /// <param name="accountSecretKey">Account Secret Key as byte[]</param>
        /// <param name="QRPixelsPerModule">Number of pixels per QR Module (2 = ~120x120px QRCode)</param>
        /// <returns>SetupCode object</returns>
        public SetupCode GenerateSetupCode(string issuer, string accountTitleNoSpaces, byte[] accountSecretKey, int QRPixelsPerModule)
        {
            if (accountTitleNoSpaces == null)
            {
                throw new NullReferenceException("Account Title is null");
            }
            accountTitleNoSpaces = RemoveWhitespace(accountTitleNoSpaces);
            string encodedSecretKey = Base32Encoding.ToString(accountSecretKey);
            string provisionUrl     = null;

            //otpauth://totp/Google:[email protected]?secret=xxxx&issuer=Google
            provisionUrl = String.Format("otpauth://totp/{2}:{0}?secret={1}&issuer={2}", accountTitleNoSpaces, encodedSecretKey.Replace("=", ""), UrlEncode(issuer));



            using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
                using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(provisionUrl, QRCodeGenerator.ECCLevel.M))
                    using (QRCode qrCode = new QRCode(qrCodeData))
                        using (Bitmap qrCodeImage = qrCode.GetGraphic(QRPixelsPerModule))
                            using (MemoryStream ms = new MemoryStream())
                            {
                                qrCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                                return(new SetupCode(accountTitleNoSpaces, encodedSecretKey, String.Format("data:image/png;base64,{0}", Convert.ToBase64String(ms.ToArray()))));
                            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Generate a setup code for a Google Authenticator user to scan
 /// </summary>
 /// <param name="issuer">Issuer ID (the name of the system, i.e. 'MyApp'), can be omitted but not recommended https://github.com/google/google-authenticator/wiki/Key-Uri-Format </param>
 /// <param name="accountTitleNoSpaces">Account Title (no spaces)</param>
 /// <param name="accountSecretKey">Account Secret Key</param>
 /// <param name="QRPixelsPerModule">Number of pixels per QR Module (2 pixels give ~ 100x100px QRCode)</param>
 /// <returns>SetupCode object</returns>
 public SetupCode GenerateSetupCode(string issuer, string accountTitleNoSpaces, string accountSecretKey, int QRPixelsPerModule)
 {
     //byte[] key = Encoding.UTF8.GetBytes(accountSecretKey);
     byte[] key = Base32Encoding.ToBytes(accountSecretKey);
     return(GenerateSetupCode(issuer, accountTitleNoSpaces, key, QRPixelsPerModule));
 }
Exemplo n.º 3
0
 internal string GenerateHashedCode(string secret, long iterationNumber, int digits = 6)
 {
     // byte[] key = Encoding.UTF8.GetBytes(secret);
     byte[] key = Base32Encoding.ToBytes(secret);
     return(GenerateHashedCode(key, iterationNumber, digits));
 }