MediaWiki  master
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 
56  return self::$algo;
57  }
58  }
59 
60  // We only reach here if no acceptable hash is found in the list, this should
61  // be a technical impossibility since most of php's hash list is fixed and
62  // some of the ones we list are available as their own native functions
63  // But since we already require at least 5.2 and hash() was default in
64  // 5.1.2 we don't bother falling back to methods like sha1 and md5.
65  throw new DomainException( "Could not find an acceptable hashing function in hash_algos()" );
66  }
67 
76  public static function hashLength( $raw = true ) {
77  $raw = (bool)$raw;
78  if ( is_null( self::$hashLength[$raw] ) ) {
79  self::$hashLength[$raw] = strlen( self::hash( '', $raw ) );
80  }
81 
82  return self::$hashLength[$raw];
83  }
84 
93  public static function hash( $data, $raw = true ) {
94  return hash( self::hashAlgo(), $data, $raw );
95  }
96 
106  public static function hmac( $data, $key, $raw = true ) {
107  if ( !is_string( $key ) ) {
108  // a fatal error in HHVM; an exception will at least give us a stack trace
109  throw new InvalidArgumentException( 'Invalid key type: ' . gettype( $key ) );
110  }
111  return hash_hmac( self::hashAlgo(), $data, $key, $raw );
112  }
113 
114 }
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:76
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:93
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
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
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 hashAlgo()
Decide on the best acceptable hash algorithm we have available for hash()
Definition: MWCryptHash.php:44
static hmac( $data, $key, $raw=true)
Generate an acceptably unstable one-way-hmac of some text making use of the best hash algorithm that ...