Ejemplo n.º 1
0
        public String getOtpNum(String extraInfo, long?systemCurrentTimeMillis = null)
        {
            log("getOtpNum extraInfo" + extraInfo);
            String tid4Location = getTid4Location();

            if (string.IsNullOrEmpty(tid4Location))
            {
                log("getOtpNum tid is null");
                //logGetOtp(null, null, str);
                return(null);
            }
            string encryptedSeed;
            String settingInfo;

            byte[] OtpNumseed;
            //String key = OtpShareStore.SETTING_INFOS_NEW;
            String lastSeed = OtpShareStore.getString(this.applicationContex, tid4Location, OtpShareStore.SETTING_INFOS_NEW);

            log("获取本地加密的种子1 encryptedSeed =" + lastSeed);
            if (string.IsNullOrEmpty(lastSeed))
            {
                //key = OtpShareStore.SETTING_INFOS;
                encryptedSeed = OtpShareStore.getString(this.applicationContex, tid4Location, OtpShareStore.SETTING_INFOS);
                settingInfo   = OtpShareStore.SETTING_INFOS;
            }
            else
            {
                encryptedSeed = lastSeed;
                settingInfo   = OtpShareStore.SETTING_INFOS_NEW;
            }

            log("获取本地加密的种子2 encryptedSeed =" + encryptedSeed + " settingInfo=" + settingInfo);
            if (encryptedSeed == null || "" == encryptedSeed)
            {
                log("getOtpNum encryptedSeed is null");
                OtpNumseed = null;
            }
            else
            {
                lastSeed = tid4Location.GetJavaHashCode().ToString();
                if (settingInfo == (OtpShareStore.SETTING_INFOS))
                {
                    lastSeed = Guid.NewGuid().ToString();
                    //lastSeed = DeviceInfo.getInstance().getImei();
                }
                byte[] decryptOtpSeed = OtpSeedCryptor.decryptOtpSeed(encryptedSeed, lastSeed);
                if (decryptOtpSeed == null)
                {
                    log("机密种子seed is null");
                    deleteSeed();
                    OtpNumseed = decryptOtpSeed;
                }
                else
                {
                    OtpNumseed = decryptOtpSeed;
                }
            }
            if (OtpNumseed == null)
            {
                log("getOtpNum seed is null");
                //logGetOtp(null, null, str);
                return(null);
            }
            byte[] bArr;
            if (String.IsNullOrEmpty(extraInfo))
            {
                bArr = OtpNumseed;
            }
            else
            {
                log("init seed: " + bytesToHexString(OtpNumseed));
                log("extraInfo : " + extraInfo);
                byte[] a = sha1(extraInfo);
                bArr = new byte[(a.Length + OtpNumseed.Length)];
                Array.Copy(OtpNumseed, 0, bArr, 0, OtpNumseed.Length);
                Array.Copy(a, 0, bArr, OtpNumseed.Length, a.Length);
                log("extraBytes : " + bytesToHexString(a));
                log("final seed :" + bytesToHexString(bArr));
            }

            try
            {
                var    timeDiff    = getServerTimeDiff();
                string intervalStr = OtpShareStore.getString(this.applicationContex, "interval", settingInfo);
                long   interval    = !string.IsNullOrEmpty(intervalStr) ? long.Parse(intervalStr) : 30;

                var currentTimeMillis =
                    systemCurrentTimeMillis.HasValue?
                    systemCurrentTimeMillis.Value:
                    (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;

                long unixTimestamp = !string.IsNullOrEmpty(timeDiff) ? (currentTimeMillis / 1000) + long.Parse(timeDiff) : currentTimeMillis / 1000;

                if (settingInfo == (OtpShareStore.SETTING_INFOS))
                {
                    try
                    {
                        lastSeed = OtpShareStore.getString(this.applicationContex, getUserId() + "+" + tid4Location, settingInfo);

                        OtpShareStore.putString(this.applicationContex, "interval", interval.ToString(), OtpShareStore.SETTING_INFOS_NEW);
                        OtpShareStore.putString(this.applicationContex, getUserId() + "+" + tid4Location, lastSeed, OtpShareStore.SETTING_INFOS_NEW);
                        OtpShareStore.putString(this.applicationContex, "timedeff", timeDiff, OtpShareStore.SETTING_INFOS_NEW);

                        lastSeed = bytesToHexString(bArr);
                        log(" strOtpSeedHex=" + lastSeed);

                        lastSeed = OtpSeedCryptor.encryptOtpSeed(lastSeed, tid4Location.GetJavaHashCode().ToString());
                        log(" decodeSeed=" + lastSeed);

                        OtpShareStore.putString(this.applicationContex, tid4Location, lastSeed, OtpShareStore.SETTING_INFOS_NEW);
                    }
                    catch (Exception e)
                    {
                    }
                }

                return(Hotp.Compute(bArr, unixTimestamp / interval, 6));
            }
            catch (Exception e)
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
 private byte[] sha1(string str)
 {
     return(OtpSeedCryptor.Sha1(str));
 }