简体   繁体   English

Bcrypt vs Hash in laravel

[英]Bcrypt vs Hash in laravel

I want to create a function or something like a Cron that executes a link (in Laravel), with something like a password. 我想创建一个函数或类似Cron的东西,它执行链接(在Laravel中),类似于密码。 I've got two solutions. 我有两个解决方案。 But which one is better to use: 但哪一个更好用:

Option 1 (hash): 选项1(哈希):

<?php

// Page 1

$salt = "my-random-hash";
$key = hash('sha256', date('Y-m-d').$salt);

// <-- Insert go to page and send GET with $key code here

// Page 2

$salt = "my-random-hash";
$key = hash('sha256', date('Y-m-d').$salt);

if ($key == $pageOneKey) {
    // Execute some code
}

Option 2 (bcrypt): 选项2(bcrypt):

<?php

// Page 1

$key = Crypt::encrypt(date('Y-m-d'));

// <-- Insert go to page and send GET with $key code here

// Page 2

$key = date('Y-m-d');
$pageOneKey = Crypt::decrypt($key);

if ($key == $pageOneKey) {
    // Execute some code
}

This code has been described broadly. 该代码已被广泛描述。 With better to use i mean safer / more secure, or something in that trance. 更好地使用我意味着更安全/更安全,或在那种恍惚状态。 Thanks! 谢谢!

Your second option isn't bcrypt. 你的第二个选择不是bcrypt。 Laravel's Crypt class uses AES encryption. Laravel的Crypt类使用AES加密。
As stated in the documentation : 文档中所述

Laravel provides facilities for strong AES encryption via the Mcrypt PHP extension. Laravel通过Mcrypt PHP扩展提供强大的AES加密功能。

As far as I can tell you don't need to be able to decrypt the data, to reverse the encryption. 据我所知,你不需要能够解密数据,反转加密。 Therefore you should definitely use a hashing algorithm like sha256 in your first option. 因此,您绝对应该在第一个选项中使用像sha256这样的散列算法。 However Laravel ships with a pretty good hashing class already so why not use that. 然而,Laravel已经提供了一个非常好的哈希类,所以为什么不使用它。

Option 3 (Laravel Hash , Bcrypt) 选项3(Laravel Hash ,Bcrypt)

$hash = Hash::make('secret');

$input = 'secret';
if(Hash::check($input, $hash)){
    // the input matches the secret
}

Note that you have to use Hash::check() for comparing. 请注意 ,您必须使用Hash::check()进行比较。 You can't just create another hash with Hash::make() and compare them. 你不能只用Hash::make()创建另一个哈希并比较它们。 The generated hash contains a random component, so even if it's the same secret, Hash::make() will produce a different hash every time. 生成的哈希包含一个随机组件,因此即使它是相同的秘密, Hash::make()每次都会产生不同的哈希。

Hashing - Laravel docs Hashing - Laravel文档

If you never need to decrypt the key for further use, the first option is better. 如果您永远不需要解密密钥以供进一步使用,则第一个选项更好。

If you need to get the key back after it's been encrypted, the second option will be better. 如果你需要在密钥加密后取回密钥,第二个选项会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM