Google Authenticator类型的JSFIDDLE代码在普通浏览器中不起作用



我有此代码http://jsfiddle.net/sk1hg4h3/1/.it在JSFiddle中工作正常,但在正常浏览器中工作。我已经包括了打电话给所有外部文件

<head>
    <script src="auth.js" type="text/javascript"></script>
    <script src="sha.js" type="text/javascript"></script>
    <link rel="stylesheet"  href="bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="am.css">
    <script type="text/javascript" src="http://ajax.googleapis.co/ajax/libs/jquery/1.9.1/jquery.min.js"></script>>
</head>

请参见下面的示例,您只需要在sha.js之前包含jQuery,然后在关闭车身标签之前加载JS文件即可。

    function dec2hex(s) {
      return (s < 15.5 ? '0' : '') + Math.round(s).toString(16);
    }
    function hex2dec(s) {
      return parseInt(s, 16);
    }
    function base32tohex(base32) {
      var base32chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
      var bits = "";
      var hex = "";
      for (var i = 0; i < base32.length; i++) {
        var val = base32chars.indexOf(base32.charAt(i).toUpperCase());
        bits += leftpad(val.toString(2), 5, '0');
      }
      for (var i = 0; i + 4 <= bits.length; i += 4) {
        var chunk = bits.substr(i, 4);
        hex = hex + parseInt(chunk, 2).toString(16);
      }
      return hex;
    }
    function leftpad(str, len, pad) {
      if (len + 1 >= str.length) {
        str = Array(len + 1 - str.length).join(pad) + str;
      }
      return str;
    }
    function updateOtp() {
      var key = base32tohex($('#secret').val());
      var epoch = Math.round(new Date().getTime() / 1000.0);
      var time = leftpad(dec2hex(Math.floor(epoch / 30)), 16, '0');
      // updated for jsSHA v2.0.0 - http://caligatio.github.io/jsSHA/
      var shaObj = new jsSHA("SHA-1", "HEX");
      shaObj.setHMACKey(key, "HEX");
      shaObj.update(time);
      var hmac = shaObj.getHMAC("HEX");
      $('#qrImg').attr('src', 'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=200x200&chld=M|0&cht=qr&chl=otpauth://totp/user@host.com%3Fsecret%3D' + $('#secret').val());
      $('#secretHex').text(key);
      $('#secretHexLength').text((key.length * 4) + ' bits');
      $('#epoch').text(time);
      $('#hmac').empty();
      if (hmac == 'KEY MUST BE IN BYTE INCREMENTS') {
        $('#hmac').append($('<span/>').addClass('label important').append(hmac));
      } else {
        var offset = hex2dec(hmac.substring(hmac.length - 1));
        var part1 = hmac.substr(0, offset * 2);
        var part2 = hmac.substr(offset * 2, 8);
        var part3 = hmac.substr(offset * 2 + 8, hmac.length - offset);
        if (part1.length > 0) $('#hmac').append($('<span/>').addClass('label label-default').append(part1));
        $('#hmac').append($('<span/>').addClass('label label-primary').append(part2));
        if (part3.length > 0) $('#hmac').append($('<span/>').addClass('label label-default').append(part3));
      }
      var otp = (hex2dec(hmac.substr(offset * 2, 8)) & hex2dec('7fffffff')) + '';
      otp = (otp).substr(otp.length - 6, 6);
      $('#otp').text(otp);
    }
    function timer() {
      var epoch = Math.round(new Date().getTime() / 1000.0);
      var countDown = 30 - (epoch % 30);
      if (epoch % 30 == 0) updateOtp();
      $('#updatingIn').text(countDown);
    }
    $(function() {
      updateOtp();
      $('#update').click(function(event) {
        updateOtp();
        event.preventDefault();
      });
      $('#secret').keyup(function() {
        updateOtp();
      });
      setInterval(timer, 1000);
    });
      body {
        padding-top: 60px;
      }
      
      .container-fluid {
        min-width: 100px
      }
<div class="container-fluid">
  <div>
    <div class="row">
      <div class="span8">
        <h1>Time-based One-time Password Algorithm</h1>
        <p>This page contains a javascript implementation of the <em>Time-based One-time Password Algorithm</em> used by Google Authenticator and described in the <a href="http://tools.ietf.org/id/draft-mraihi-totp-timebased-06.html">TOTP RFC Draft</a>.</p>
        <p>Install Google Authenticator on your smartphone: <a href="http://itunes.apple.com/au/app/google-authenticator/id388497605?mt=8">iOS</a>, <a href="https://market.android.com/details?id=com.google.android.apps.authenticator&hl=en">Android</a>,
          <a href="http://m.google.com/authenticator">Blackberry</a>. As the TOTP is an open standard you can use this app to create one-time passwords for your own application. You add an account plus secret by scanning a QR code (more info on the
          <a
          href="http://code.google.com/p/google-authenticator/wiki/KeyUriFormat">google code wiki</a>). The javascript below implements the algorithm the smartphone app uses to generate the OTP - you would use this same algorithm <em>server-side</em> to verify an OTP.</p>
        <p>Put it to the test by setting the base32 secret, scanning the QR code in Google Authenticate. You should see the same OTP on your smartphone and displayed at the bottom on the page.</p>
      </div>
    </div>
    <div class="row">
      <form>
        <fieldset>
          <div class="clearfix">
            <label for="secret">Secret (base32)</label>
            <div class="input">
              <input type="text" size="30" name="secret" id="secret" class="xlarge" value="JBSWY3DPEHPK3PXP" />
            </div>
          </div>
          <!-- /clearfix -->
          <div class="clearfix">
            <label>Secret (hex)</label>
            <div class="input"><span class="label label-default" id="secretHex"></span>
              <span id='secretHexLength'></span></div>
          </div>
          <!-- /clearfix -->
          <div class="clearfix">
            <label>QR Code</label>
            <div class="input"><img id="qrImg" src="" /></div>
          </div>
          <!-- /clearfix -->
          <div class="clearfix">
            <label>Unix epoch div 30 (padded hex)</label>
            <div class="input"><span class="label label-default" id='epoch'></span></div>
          </div>
          <!-- /clearfix -->
          <div class="clearfix">
            <label>HMAC(secret, time)</label>
            <div class="input" id='hmac'></div>
          </div>
          <!-- /clearfix -->
          <div class="clearfix">
            <label>One-time Password</label>
            <div class="input"><span class="label label-primary" id='otp'></span></div>
          </div>
          <!-- /clearfix -->
          <div class="clearfix">
            <label>Updating in</label>
            <div class="input"><span id='updatingIn'></span></div>
          </div>
          <!-- /clearfix -->
        </fieldset>
      </form>
    </div>
  </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://caligatio.github.io/jsSHA/sha.js"></script>

最新更新