/** * Generate a new uuid. * * We're generating uuids according to the official v4 spec. * * @return string */ protected function generate() { $hash = bin2hex(Str::randomBytes(16)); $timeHi = hexdec(substr($hash, 12, 4)) & 0xfff; $timeHi &= ~0xf000; $timeHi |= 4 << 12; $clockSeqHi = hexdec(substr($hash, 16, 2)) & 0x3f; $clockSeqHi &= ~0xc0; $clockSeqHi |= 0x80; $params = [substr($hash, 0, 8), substr($hash, 8, 4), sprintf('%04x', $timeHi), sprintf('%02x', $clockSeqHi), substr($hash, 18, 2), substr($hash, 20, 12)]; return vsprintf('%08s-%04s-%04s-%02s%02s-%012s', $params); }
/** * Encrypt the given value. * * @param string $value * @return string */ public function encrypt($value) { $iv = Str::randomBytes($this->getIvSize()); $value = openssl_encrypt(serialize($value), $this->cipher, $this->key, 0, $iv); if ($value === false) { throw new EncryptException('Could not encrypt the data.'); } // Once we have the encrypted value we will go ahead base64_encode the input // vector and create the MAC for the encrypted value so we can verify its // authenticity. Then, we'll JSON encode the data in a "payload" array. $mac = $this->hash($iv = base64_encode($iv), $value); return base64_encode(json_encode(compact('iv', 'value', 'mac'))); }
public function encrypt($value) { $iv = Str::randomBytes($this->getIvSize()); $value = openssl_encrypt(serialize($value), $this->cipher, $this->key, 0, $iv); if ($value === false) { throw new EncryptException('Could not encrypt the data.'); } $mac = $this->hash($iv = base64_encode($iv), $value); return base64_encode(json_encode(compact('iv', 'value', 'mac'))); }
/** * Determine if the MAC for the given payload is valid. * * @param array $payload * @return bool * * @throws \RuntimeException */ protected function validMac(array $payload) { $bytes = Str::randomBytes(16); $calcMac = hash_hmac('sha256', $this->hash($payload['iv'], $payload['value']), $bytes, true); return Str::equals(hash_hmac('sha256', $payload['mac'], $bytes, true), $calcMac); }
/** * Handle the enable repo command. * * @param \StyleCI\StyleCI\Commands\Repo\EnableRepoCommand $command * * @return void */ public function handle(EnableRepoCommand $command) { $repo = Repo::create(['id' => $command->id, 'user_id' => $command->user->id, 'name' => $command->name, 'default_branch' => $command->branch, 'token' => bin2hex(Str::randomBytes(10))]); event(new RepoWasEnabledEvent($repo)); }