Created
August 6, 2014 02:14
-
-
Save froop/034806eeab339cfd9da9 to your computer and use it in GitHub Desktop.
[JS] 2-Step Password: パスワードを2段階にして覚えるのは単純な文字列で済むように http://blog.livedoor.jp/froo/archives/50906020.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>2-Step Password</title> | |
<script src="sha256.js"></script><!-- https://github.com/Caligatio/jsSHA --> | |
<script src="2StepPassword.js"></script> | |
</head> | |
<body> | |
<form id="main-form"> | |
<input id="src-text" required> | |
<button type="submit">make</button> | |
</form> | |
<div> | |
<input id="res-text" readonly> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (global) { | |
var RESULT_LENGTH = 16; | |
function hashPassword(srcText) { | |
var base64 = new jsSHA(srcText, "ASCII").getHash("SHA-256", "B64"); | |
return base64.replace(/[+/=]/g, "").substring(0, RESULT_LENGTH); | |
} | |
function onSubmit(event) { | |
event.preventDefault(); | |
var srcText = document.getElementById("src-text").value; | |
var resText = hashPassword(srcText); | |
document.getElementById("res-text").value = resText; | |
} | |
function onLoad() { | |
var mainForm = document.getElementById("main-form"); | |
mainForm.addEventListener("submit", onSubmit); | |
} | |
global.addEventListener("load", onLoad); | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment