示例#1
0
文件: functions.php 项目: effu/Wordle
/**
 * Picks a weighted random entry out of the given array and returns its key.
 *
 * For example, given the array:
 * ```
 * [
 *     'foo' => 2,
 *     'bar' => 4,
 *     'baz' => 12
 * ]
 * ```
 * It'll return `bar` about twice as often as it'll return `foo`, and `baz` about three times as often as `bar`.
 *
 * @param int[] $list Array in the form of `[key => weight]`.
 *
 * All weights **must** be whole numbers greater than or equal to zero. Total weight must exceed zero.
 *
 * @throws \InvalidArgumentException if a weight is negative or the total weight is not greater than zero.
 *
 * @return int|string The chosen key.
 */
function array_weighted_rand(array $list)
{
    $totalWeight = gmp_init(0);
    foreach ($list as $key => $weight) {
        if ($weight < 0) {
            throw new \InvalidArgumentException("Weights cannot be negative. Found {$key} => {$weight}.");
        }
        $totalWeight += $weight;
    }
    if ($totalWeight == 0) {
        throw new \InvalidArgumentException("Total weight must exceed zero.");
    } elseif ($totalWeight == 1) {
        return array_search(1, $list);
    }
    $rand = gmp_random_range(1, $totalWeight);
    foreach ($list as $key => $weight) {
        $rand -= $weight;
        if ($rand <= 0) {
            return $key;
        }
    }
}
示例#2
0
<?php

include "database_connect.php";
$uid = gmp_intval(gmp_random_range(1000000, 9999999));
$username = $_POST['username'];
$email = $_POST['email'];
$password_raw = $_POST['password'];
$password_hashed = password_hash($password_raw, PASSWORD_DEFAULT);
$sql_search_username = "******";
$sql_search_email = "select uid from users where email='{$email}';";
$result1 = $mysqli->query($sql_search_username);
$result2 = $mysqli->query($sql_search_email);
if ($result1->num_rows == 0 && $result2->num_rows == 0) {
    $sql_insert_user = "******";
    if ($mysqli->query($sql_insert_user) === false) {
        echo mysqli_error($mysqli);
        die($sql_insert_user);
    }
    $table_name = 'contact_' . $uid;
    $sql_create_table = 'create table ' . $table_name . ' (
		ID int(11) auto_increment primary key,
		firstname varchar(256) null,
		lastname varchar(256) null,
		gender varchar(256) null,
		phone varchar(256) null,
		phoneRaw varchar(256) null,
		phoneType varchar(256) null,
		addressLineOne varchar(256) null,
		addressLineTwo varchar(256) null,
		city varchar(256) null,
		state varchar(256) null,
示例#3
0
 public function testDecodeWithDiffrentPasswordsProducesDiffrentResults()
 {
     $message = '[%s / %d rounds] Same base %d value decoded with diffrent passwords produce diffrent results [%s].';
     foreach (self::$testedCiphers as $cipher => $usesIv) {
         foreach (self::$testedRounds as $rounds => $description) {
             $cryptomute = $this->getCryptomute($cipher, $rounds);
             foreach (Cryptomute::$allowedBases as $base => $pattern) {
                 for ($i = 0; $i < self::TEST_REPEATS; $i++) {
                     $iv = $usesIv ? openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher)) : null;
                     $input = gmp_strval(gmp_random_range(gmp_init(self::MIN_VALUE, 10), gmp_init(self::MAX_VALUE, 10)), $base);
                     $decrypted1 = $cryptomute->decrypt($input, $base, false, 'foo', $iv);
                     $decrypted2 = $cryptomute->decrypt($input, $base, false, 'bar', $iv);
                     $this->assertNotEquals($decrypted1, $decrypted2, sprintf($message, $cipher, $rounds, $base, $input));
                 }
             }
         }
     }
 }