/ src / scripts / hash_captcha.js
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026  Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Chris Pollett chris@pollett.org
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
/*
 * Finds the nonce for the input parameters given by the server.
 *
 * @param Object nonce_for_string a DOM element to put the value of a nonce
 * @param Object random_string a DOM element to get the value of random_string
 * @param Object time a DOM element to get the value of the time
 * @param Object level a DOM element to get the value of a level
 */
function findNonce(nonce_for_string, random_string, time, level)
{
    var random_field = elt(random_string);
    var time_field = elt(time);
    var level_field = elt(level);
    var nonce_field = elt(nonce_for_string);
    /* A page whose form asks no puzzle carries none of these boxes,
       and reading a value off one that is not there stops the rest of
       the page's script where it stands: the line that tells somebody
       what just happened runs in the same breath, so a page that threw
       here went quiet rather than saying anything. */
    if (!random_field || !time_field || !level_field || !nonce_field ||
        !random_field.value) {
        return;
    }
    var nonce = hashStamp(random_field.value, time_field.value,
        level_field.value);
    nonce_field.setAttribute('value', nonce);
    nonce_field.setAttribute('type', 'hidden');
}
/*
 * This function calculates the sha1 of a string until
 * number of a leading zeroes in the sha1 value matchesa level
 * parameter.
 *
 * @param String random_string a string sent by the server
 * @param String time the time sent by the server
 * @param String level define number of leading zeroes
 * @return int nonce for which the sha1 of a string
 *     produces the level number of a zeroes
 */
function hashStamp(random_string, time, level)
{
    var nonce = -1;
    var input_string = '';
    var pattern = new RegExp("^0{"+level+"}");
    while (!generateSha1(input_string).match(pattern)) {
        nonce = nonce + 1;
        input_string = random_string +':'+time + ':' + nonce;
    }
    return nonce;
}
X