Smart.Framework Logo

final class \SmartHashCrypto
{ } ::

Class: SmartHashCrypto - provide various hashes for a string: salted password, sha512, sha384, sha256, sha1, md5.
Hints:
- hashing passwords: is better to prepend the secret, the input is unknown to attackers so these kind of hashes are safe against length attacks ; they have to be protected against colissions ... where more different inputs can generate the same hash !
- hashing checksum: they MUST append the secret to the text to real protect against length attacks where both the input and the hash are public # https://en.wikipedia.org/wiki/Length_extension_attack


class Methods

public static function checksum ( string $y_data, string $y_custom_salt = null ) {} :: STRING
@return: {STRING} The checksum hash as B62 using the hex SHA384 as data + 'salt' suffix (append) ; ~ 43 bytes length
@param: {STRING} $y_data: The data to be hashed
@param: {STRING} $y_custom_salt: The salt (will be trimmed from whitespaces) ; If the salt is empty will use a combination of SMART_SOFTWARE_NAMESPACE and SMART_FRAMEWORK_SECURITY_KEY constants as the salt because the checksum must use a mandatory unpredictable salt appended to the data to prevent the length extension attack
Create a safe checksum of data
It will append the salt to the end of data to avoid the length extension attack # https://en.wikipedia.org/wiki/Length_extension_attack
Protected by SHA384 that has 128-bit resistance against the length extension attacks since the attacker needs to guess the 128-bit to perform the attack, due to the truncation
public static function password ( string $y_pass, string $y_custom_salt = "" ) {} :: STRING
@return: {STRING} The password hash: a 128 bytes padded with * on right, composed from 98 bytes hash with a prefix and the SHA512 (base64, 88 bytes)
@param: {STRING} $y_pass: The password
@param: {STRING} $y_custom_salt: The salt (default is empty)
Encrypt (one way) a password by creating a safe password hash
It uses a custom salt + an internally hard-coded salt to avoid rainbow attack
public static function checkpassword ( string $y_hash, string $y_pass, string $y_custom_salt = "" ) {} :: BOOL
@return: {BOOL} Will return TRUE if password match or FALSE if not
@param: {STRING} $y_hash: The password hash to be checked
@param: {STRING} $y_pass: The password
@param: {STRING} $y_custom_salt: The salt (default is empty)
Check a password hash provided by SmartHashCrypto::password()
It must use the same salt as it was used when password was hashed ; if not using version detect the same version must be used as used when hashed
public static function sha512 ( string $y_str, bool $y_base64 = false ) {} :: STRING
@return: {STRING} The hash: 128 chars length (hex) or 88 chars length (b64)
@param: {STRING} $y_str: String to be hashed
@param: {BOOLEAN} $y_base64: If set to TRUE will use Base64 Encoding instead of Hexa Encoding
Returns the SHA512 hash of a string
public static function sha384 ( string $y_str, bool $y_base64 = false ) {} :: STRING
@return: {STRING} The hash: 96 chars length (hex) or 64 chars length (b64)
@param: {STRING} $y_str: String to be hashed
@param: {BOOLEAN} $y_base64: If set to TRUE will use Base64 Encoding instead of Hexa Encoding
Returns the SHA384 hash of a string
SHA384 is roughly 50% faster than SHA-256 on 64-bit machines
SHA384 has resistances to length extension attack but SHA512 doesn't have
SHA384 128-bit resistance against the length extension attacks is because the attacker needs to guess the 128-bit to perform the attack, due to the truncation
public static function sha256 ( string $y_str, bool $y_base64 = false ) {} :: STRING
@return: {STRING} The hash: 64 chars length (hex) or 44 chars length (b64)
@param: {STRING} $y_str: String to be hashed
@param: {BOOLEAN} $y_base64: If set to TRUE will use Base64 Encoding instead of Hexa Encoding
Returns the SHA256 hash of a string
public static function sha1 ( string $y_str, bool $y_base64 = false ) {} :: STRING
@return: {STRING} The hash: 40 chars length (hex) or 28 chars length (b64)
@param: {STRING} $y_str: String to be hashed
@param: {BOOLEAN} $y_base64: If set to TRUE will use Base64 Encoding instead of Hexa Encoding
Returns the SHA1 hash of a string
public static function md5 ( string $y_str, bool $y_base64 = false ) {} :: STRING
@return: {STRING} The hash: 32 chars length (hex) or 24 chars length (b64)
@param: {STRING} $y_str: String to be hashed
@param: {BOOLEAN} $y_base64: If set to TRUE will use Base64 Encoding instead of Hexa Encoding
Returns the MD5 hash of a string
public static function crc32b ( string $y_str, bool $y_base36 = false ) {} :: STRING
@return: {STRING} The hash: 8 chars length (hex) or 7 chars length (b36)
@param: {STRING} $y_str: String to be hashed
@param: {BOOLEAN} $y_base36: If set to TRUE will use Base36 Encoding instead of Hexa Encoding
Returns the CRC32B hash of a string in base16 by default or base36 optional (better than CRC32, portable between 32-bit and 64-bit platforms, unsigned)

class Properties


class Constants

public const PASSWORD_HASH_LENGTH = 128 ;  ::

Sample code: PHP

<?php

// Usage example:
 
SmartHashCrypto::some_method_of_this_class(...);

// #end php code


documentation generated on: 2023-10-19 23:15:44 +0000


Smart.Framework © 2009-2023 unix-world.org