简体   繁体   English

在PHP中将SHA1加密和SHA1转换为十六进制

[英]SHA1 encryption and SHA1 to Hex in php

I have two methods in Java and I want to do the same methods in php. 我在Java中有两种方法,但我想在php中做同样的方法。 I don't know absolutely nothing in php. 我绝对不知道php中什么都没有。 How can I do it? 我该怎么做?

Method 1: 方法1:

public static String encyptPassword (String in) throws UnsupportedEncodingException, NoSuchAlgorithmException{
    byte[] bytes=in.getBytes("UTF-8");
    MessageDigest md=MessageDigest.getInstance(MGF1ParameterSpec.SHA1.getDigestAlgorithm());
    md.update(bytes);
    byte[] digest=md.digest();
    return toHex(digest);
}

Method 2: 方法2:

public static String toHex(byte[] bytes) {
    BigInteger bi = new BigInteger(1, bytes);
    return String.format("%0" + (bytes.length << 1) + "x", bi);
}

The methods (function?) in php must have the same result as in java, because it's hashing passwords for a working and online login system. php中的方法(函数?)必须与java中的方法具有相同的结果,因为它是工作中和在线登录系统的哈希密码。

I'm trying it about 3 hours, but I can't do it or found a solution. 我正在尝试大约3个小时,但无法执行或找不到解决方案。 I think I read all posts on Stack. 我想我阅读了Stack上的所有文章。 Can you help me? 你能帮助我吗? Thanks. 谢谢。

PHP Fiddle - hit run to see the result PHP Fiddle- 点击运行即可查看结果

<?php

    $pass = 'MySecretP@55';
    $hashed = hash("SHA512", $pass);
    echo $hashed;
    echo '<hr>' . bin2hex($hashed);

?>

Above is sha512 , which is certainly better that sha1 , and bcrypt with reasonably high cost is considered as the best currently 上面是sha512 ,肯定比sha1更好,并且价格合理的bcrypt被认为是目前最好的

  1. Hashing != Encryption 哈希!=加密
  2. SHA1 is weak, SHA2 is better, bcrypt is currently the best generally-available hashing algorithm for password storage. SHA1较弱,SHA2较好,bcrypt是目前用于存储密码的最佳通用哈希算法。
  3. $myHash = hash("SHA1", "foobar") Docs $myHash = hash("SHA1", "foobar") 文档
  4. Don't use #3, use $myActuallySecureHash = password_hash("foobar") Docs 不要使用#3,请使用$myActuallySecureHash = password_hash("foobar") 文档
  5. Use #4. 使用#4。
  6. PHP < 5.4 is not an excuse. PHP <5.4不是借口。

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

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