[英]Unclear code in Javascript
我花了4个小时试图弄清楚以下代码中发生了什么。 因为我还没有编写Java脚本,所以我很难理解。
Javascript:
function pincheck() {
var n = 123456789123,
t = md5(n + " " + $("#wert").val()),
e = $("input[name='some_hash']").val();
$.post("https://url-test.com/site?check=" + t, {some_hash: e}, function(n) {
$("#inputWert").html(n == t ? "<span style='color:green; font-weight:bold'>OK</span>"
: "<span style='color:red; font-weight:bold'>NOT OK</span>")
})
}
对应的HTML形式:
<form action="https://url-test.com/site" method="post" accept-charset="utf-8">
<div style="display:none">
<input type="hidden" name="some_hash" value="2cb6beab7ac4240043b20674a3dce6a5" />
</div>
<input type="text" id="wert" name="wert" placeholder="WERT" onchange="pincheck()">
<div id="inputWert">Please input</div>
<input type="submit" value="Submit" />
<h2>Please explain<br/><textarea name="explain" style="width: 650px;height: 100px;" placeholder="Explanation"></textarea><br/>
</form>
我的想法是:
我的问题背景涉及我们大学的IT安全任务。 我们必须猜测一些数字,然后将其输入文本形式,我想谁的id应该是“ wert”。
我希望有一个人可以帮助我。 先感谢您!
当用户在WERT
输入字段中键入内容时,Javascript将接受用户的输入,将123456789123
放在其开头,计算出它的MD5哈希值,并将其分配给t
。 e
设置为隐藏的some_hash
值的内容。
然后,它执行AJAX查询,将t
和e
发送到url-test.com/site
脚本。 t
在check
URL参数中发送,而e
在some_hash
POST数据中发送。
服务器脚本返回一个字符串。 如果该字符串与t
匹配,则显示绿色的OK
。 如果不匹配,则会显示红色的NOT OK
。
我的猜测是,这是CAPTCHA测试的一部分。 隐藏的输入是指示显示哪个图像的代码。 然后,在服务器上,可以使用此代码在图像中查找文本的MD5哈希。
简单来说,JavaScript设置了3个变量(使用var关键字),然后使用jquery进行了HTTPS POST调用。
像这样逐行绘制代码:
// Set n to a seemingly arbitrary number
var n = 123456789123;
// Set t to an MD5 hash using n and whatever the value of the "wert" element is
var t = md5(n + " " + $("#wert").val());
// Set e to the value of the element with a name of "some_hash"
var e = $("input[name='some_hash']").val();
// Makes a POST call to a URL built using the above variables
// Format $.post(URL, data(in JSON format), callback function)
$.post(
"https://url-test.com/site?check=" + t,
{some_hash: e},
function(n) {
// Set the HTML body of the "wert" element
// If n (returned by the POST call) is equal to t, set font color to green, otherwise set font color to red
$("#inputWert").html(n == t ? "<span style='color:green; font-weight:bold'>OK</span>" :
"<span style='color:red; font-weight:bold'>NOT OK</span>")
}
);
让我们分几段代码:“ pincheck”函数通过设置三个变量开始:n(123456789123),t。 它使用javascript MD5哈希函数,其初始字符串为n +一些空格+您在表单的“ wert”字段中输入的任何值。 最后,您检索名为“ some_hash”的隐藏字段的值,并将其存储在变量“ e” /中
然后,您向服务器发起一个发布,传递键值对“ some_hash”(键)和隐藏字段的值。
当帖子返回时,它将在post语句的末尾调用匿名函数,将帖子操作返回的值作为(匿名函数的本地变量)传入“ n”。 该“ n”是与在pincheck函数打开时定义的变量不同的变量。
然后,匿名函数将检查从post操作返回的值是否与使用javascript MD5函数计算出的值相同。 如果它们相同,则显示第一个span标签;如果它们不相等,则显示第二个span标签。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.