Movatterモバイル変換


[0]ホーム

URL:


MediaWiki master
MWCryptHash.php
Go to the documentation of this file.
1<?php
13classMWCryptHash {
17protectedstatic ?string$algo =null;
18
22protectedstaticint$hashLength;
23
28publicstaticfunctionhashAlgo() {
29 $algorithm = self::$algo;
30if ( $algorithm !==null ) {
31return $algorithm;
32 }
33
34 $algos = hash_hmac_algos();
35 $preference = ['whirlpool','sha256' ];
36
37foreach ( $preference as $algorithm ) {
38if ( in_array( $algorithm, $algos,true ) ) {
39 self::$algo = $algorithm;
40return $algorithm;
41 }
42 }
43
44thrownew DomainException('Could not find an acceptable hashing function.' );
45 }
46
55publicstaticfunctionhashLength( $raw =true ) {
56 self::$hashLength ??= strlen( self::hash('',true ) );
57// Optimisation: Skip computing the length of non-raw hashes.
58// The algos in hashAlgo() all produce a digest that is a multiple
59// of 8 bits, where hex is always twice the length of binary byte length.
60return $raw ? self::$hashLength : self::$hashLength * 2;
61 }
62
71publicstaticfunctionhash( $data, $raw =true ) {
72returnhash( self::hashAlgo(), $data, $raw );
73 }
74
84publicstaticfunctionhmac( $data, $key, $raw =true ) {
85if ( !is_string( $key ) ) {
86// hash_hmac tolerates non-string (would return null with warning)
87thrownew InvalidArgumentException('Invalid key type: ' . get_debug_type( $key ) );
88 }
89return hash_hmac( self::hashAlgo(), $data, $key, $raw );
90 }
91
92}
MWCryptHash
Utility functions for generating hashes.
DefinitionMWCryptHash.php:13
MWCryptHash\hashAlgo
static hashAlgo()
Decide on the best acceptable hash algorithm we have available for hash()
DefinitionMWCryptHash.php:28
MWCryptHash\hashLength
static hashLength( $raw=true)
Return the byte-length output of the hash algorithm we are using in self::hash and self::hmac.
DefinitionMWCryptHash.php:55
MWCryptHash\$hashLength
static int $hashLength
The number of bytes outputted by the hash algorithm.
DefinitionMWCryptHash.php:22
MWCryptHash\$algo
static string $algo
The hash algorithm being used.
DefinitionMWCryptHash.php:17
MWCryptHash\hmac
static hmac( $data, $key, $raw=true)
Generate a keyed cryptographic hash value (HMAC) for a string, making use of the best hash algorithm ...
DefinitionMWCryptHash.php:84
MWCryptHash\hash
static hash( $data, $raw=true)
Generate a cryptographic hash value (message digest) for a string, making use of the best hash algori...
DefinitionMWCryptHash.php:71

[8]ページ先頭

©2009-2025 Movatter.jp