Wrapper around SODIUM_CRypto_generichash()

Returns raw binary.

@param string $input
@param int $length
@return string
@throws CannotPerformOperation
@throws \SodiumException
438377
public static function raw_hash(
        string $input,
        int $length = \SODIUM_CRYPTO_GENERICHASH_BYTES
    ): string {
        return self::raw_keyed_hash($input, '', $length);
    }
309895
public static byte[] sha1(@NotNull String input, @Nullable byte[] salt, int iterations) {
		return digest(input.getBytes(Charsets.UTF_8), get(SHA_1_DIGEST), salt, iterations);
	}
1044397
def sodium_pad(s, blocksize):
    
    ensure(isinstance(s, bytes),
           raising=exc.TypeError)
    ensure(isinstance(blocksize, integer_types),
           raising=exc.TypeError)
    if blocksize <= 0:
        raise exc.ValueError
    s_len = len(s)
    m_len = s_len + blocksize
    buf = ffi.new("unsigned char []", m_len)
    p_len = ffi.new("size_t []", 1)
    ffi.memmove(buf, s, s_len)
    rc = lib.sodium_pad(p_len, buf, s_len, blocksize, m_len)
    ensure(rc == 0, "Padding failure", raising=exc.CryptoError)
    return ffi.buffer(buf, p_len[0])[:]
469944
public function encrypt($input)
    {
        if (! $input) {
            return $input;
        }

        $methods = $this->getEncryptionMethods();
        $method  = reset($methods);
        $mkey    = key($methods);

        if ('mcrypt' == $method) {
            $result = $this->encryptMcrypt($input);
        } elseif ('null' == $method) {
            $result = $input;
        } else {
            $result = $this->encryptOpenSsl($input, $method);
        }

        return ":$mkey:" . base64_encode($result);
    }
251583
public static byte[] hash(byte[] input, int offset, int length) {
        MessageDigest digest = newDigest();
        digest.update(input, offset, length);
        return digest.digest();
    }
331650
public function verifyHash($input)
    {
        $salt = substr($input, 0, strrpos($input, '$') + 1);
        $checksum = substr($input, strrpos($input, '$') + 1);

        return ($this->verifySalt($salt) && 1 === preg_match('/^[\.\/0-9A-Za-z]{28}$/', $checksum));
    }
634449
public function salt(int $length = 32): string {
    try {
      return random_bytes($length);
    } catch(Exception $e) {
      throw new RuntimeException('Could not generate crytographically secure salt', 0, $e);
    }
  }
854414
public function unserialize($binary, $length)
    {
        list($r, $s) = str_split(unpack('H*', $binary)[1], $length);

        return new Signature(gmp_init($this->adapter->hexDec($r), 10), gmp_init($this->adapter->hexDec($s), 10));
    }
449257
private function _returnHash(string $hash, int $length)
    {
        try {
            return substr($hash, 0, $length);
        } catch (\Exception $e) { }

        return false;
    }
898657
public static function generateSalt($length = 32)
	{
		$salt = '';

		for($i = 0; $i < $length; ++$i)
			$salt .= chr(rand(0,127));

		return $salt;
	}
1201
private function newKey($hash, $length, $key = '')
  {
    $generated = $this->hasReq(Hash::secure($length));

    if ($hash == 'base64') {
      $key = 'base64:' . base64_encode($generated);
    } else {
      $key = $hash . ':' . $generated;
    }

    return $key;
  }
132264
public static String hashSHA256asStringHex(String input, int salt) throws NoSuchAlgorithmException {
		byte[] in = input.getBytes();
		ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE + in.length);
		bb.putInt(salt);
		bb.put(input.getBytes());
		byte[] output = Hash.hashSHA256(bb.array());
		StringBuilder sb = new StringBuilder("0x");
		 for (byte b : output) {
		    sb.append(hexDigit[(b >> 4) & 0x0f]);
		    sb.append(hexDigit[b & 0x0f]);
		 }
		 return sb.toString();
	}
1158402
public static function rawToHex($raw, $length = 0)
    {
        $gmp = gmp_import($raw);
        $hex = gmp_strval($gmp, 16);

        return self::pad($hex, $length);
    }
172481
def hash_input(inpt, algo=HASH_SHA256):
    """"""
    if (algo == HASH_MD5):
        hashcode = hashlib.md5()
    elif (algo == HASH_SHA1):
        hashcode = hashlib.sha1()
    elif (algo == HASH_SHA224):
        hashcode = hashlib.sha224()
    elif (algo == HASH_SHA256):
        hashcode = hashlib.sha256()
    elif (algo == HASH_SHA384):
        hashcode = hashlib.sha384()
    elif (algo == HASH_SHA512):
        hashcode = hashlib.sha512()

    if sys.version_info.major == 2:
        inpt = bytes(inpt)
    else:
        inpt = bytes(inpt, "utf-8")

    hashcode.update(inpt)
    hexhash = hashcode.hexdigest()
    return hexhash
475626
public function genSalt($input = null)
    {
        if (!$input) {
            $input = $this->_getRandomBytes(6);
        }

        // Hash identifier
        $identifier = $this->_phpBBCompat ? 'H' : 'P';

        // Cost factor
        $costFactor = $this->_itoa64[min($this->_iterationCountLog2 + 5, 30)];

        // Salt string
        $salt = $this->_encode64($input, 6);

        return '$' . $identifier . '$' . $costFactor . $salt;
    }
599813
public function decode($input)
    {
        $this->value = trim(
            mcrypt_decrypt(
                MCRYPT_RIJNDAEL_256,
                $this->secureKey,
                base64_decode($input),
                MCRYPT_MODE_ECB,
                $this->iv
            )
        );

        return $this->value;
    }
834870
protected function createByte($input, $bytes = 4, $isHex = true)
    {
        if ($isHex) {
            $byte = hex2bin(str_pad($input, $bytes * 2, '00'));
        } else {
            $byte = str_pad($input, $bytes, "\x00");
        }

        return $byte;
    }