Returns a string of random bytes.
public static generateRandomBytes ( integer $count ) : string | ||
$count | integer | Number of bytes to generate |
리턴 | string | Random bytes |
/** * Returns the encryption key from the persistent cache or Data/Persistent directory. If none exists, a new * encryption key will be generated and stored in the cache. * * @return string The configured encryption key stored in Data/Persistent/EncryptionKey */ protected function getEncryptionKey() { if ($this->encryptionKey === null) { $this->encryptionKey = $this->cache->get('encryptionKey'); } if ($this->encryptionKey === false && file_exists(FLOW_PATH_DATA . 'Persistent/EncryptionKey')) { $this->encryptionKey = file_get_contents(FLOW_PATH_DATA . 'Persistent/EncryptionKey'); } if ($this->encryptionKey === false) { $this->encryptionKey = bin2hex(Utility\Algorithms::generateRandomBytes(48)); $this->cache->set('encryptionKey', $this->encryptionKey); } return $this->encryptionKey; }
/** * @test */ public function generateRandomBytesGeneratesRandomBytes() { $this->assertEquals(20, strlen(Algorithms::generateRandomBytes(20))); }
/** * Hash a password for storage using PBKDF2 and the configured parameters. * Will use a combination of a random dynamic salt and the given static salt. * * @param string $password Cleartext password that should be hashed * @param string $staticSalt Static salt that will be appended to the random dynamic salt * @return string A Base64 encoded string with the derived key (hashed password) and dynamic salt */ public function hashPassword($password, $staticSalt = null) { $dynamicSalt = UtilityAlgorithms::generateRandomBytes($this->dynamicSaltLength); $result = CryptographyAlgorithms::pbkdf2($password, $dynamicSalt . $staticSalt, $this->iterationCount, $this->derivedKeyLength, $this->algorithm); return base64_encode($dynamicSalt) . ',' . base64_encode($result); }