MediaWiki  1.27.2
MWCryptHash.php
Go to the documentation of this file.
1 <?php
26 class MWCryptHash {
30  protected static $algo = null;
31 
35  protected static $hashLength = [
36  true => null,
37  false => null,
38  ];
39 
44  public static function hashAlgo() {
45  if ( !is_null( self::$algo ) ) {
46  return self::$algo;
47  }
48 
49  $algos = hash_algos();
50  $preference = [ 'whirlpool', 'sha256', 'sha1', 'md5' ];
51 
52  foreach ( $preference as $algorithm ) {
53  if ( in_array( $algorithm, $algos ) ) {
54  self::$algo = $algorithm;
55  wfDebug( __METHOD__ . ': Using the ' . self::$algo . " hash algorithm.\n" );
56 
57  return self::$algo;
58  }
59  }
60 
61  // We only reach here if no acceptable hash is found in the list, this should
62  // be a technical impossibility since most of php's hash list is fixed and
63  // some of the ones we list are available as their own native functions
64  // But since we already require at least 5.2 and hash() was default in
65  // 5.1.2 we don't bother falling back to methods like sha1 and md5.
66  throw new DomainException( "Could not find an acceptable hashing function in hash_algos()" );
67  }
68 
77  public static function hashLength( $raw = true ) {
78  $raw = (bool)$raw;
79  if ( is_null( self::$hashLength[$raw] ) ) {
80  self::$hashLength[$raw] = strlen( self::hash( '', $raw ) );
81  }
82 
83  return self::$hashLength[$raw];
84  }
85 
94  public static function hash( $data, $raw = true ) {
95  return hash( self::hashAlgo(), $data, $raw );
96  }
97 
107  public static function hmac( $data, $key, $raw = true ) {
108  if ( !is_string( $key ) ) {
109  // a fatal error in HHVM; an exception will at least give us a stack trace
110  throw new InvalidArgumentException( 'Invalid key type: ' . gettype( $key ) );
111  }
112  return hash_hmac( self::hashAlgo(), $data, $key, $raw );
113  }
114 
115 }
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
static hash($data, $raw=true)
Generate an acceptably unstable one-way-hash of some text making use of the best hash algorithm that ...
Definition: MWCryptHash.php:94
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
static $hashLength
The number of bytes outputted by the hash algorithm.
Definition: MWCryptHash.php:35
static $algo
The hash algorithm being used.
Definition: MWCryptHash.php:30
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
static hashLength($raw=true)
Return the byte-length output of the hash algorithm we are using in self::hash and self::hmac...
Definition: MWCryptHash.php:77
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static hmac($data, $key, $raw=true)
Generate an acceptably unstable one-way-hmac of some text making use of the best hash algorithm that ...
static hashAlgo()
Decide on the best acceptable hash algorithm we have available for hash()
Definition: MWCryptHash.php:44