dragon8837 2017-03-29 09:48
浏览 54
已采纳

Java中的Symfony FOSUser哈希算法

hey everyone am trying to find out which algorithm of hash is used my symfony FOS User Bundle I've done some research , and it's mentioned that FOSUser Bundle default security config uses Sha512() and itirate it over 5000 times + salt then bas64 encoding i'm actually new with these hash algorithms ,however this is the algorithm in php

$password = 'toto';
$salt = '1234';
$salted = $password.'{'.$salt.'}';
$digest = hash('sha512', $salted, true);

for ($i=1; $i<5000; $i++) {
  $digest = hash('sha512', $digest.$salted, true);
}

$encodedPassword = base64_encode($digest); 
}

taking from this post How do I generate a SALT in Java for Salted-Hash?

since am not familiar with java hash libraries can anyone help me how to translated this code into Java !

  • 写回答

2条回答 默认 最新

  • duanjiagu0655 2017-03-29 10:41
    关注

    Symfony default setting for password encryption is Bcrypt this code mentioned in your security.yml config file

    encoders:
        Symfony\Component\Security\Core\User\User:
            algorithm: bcrypt
            cost:      15
    

    in my case i used a trick since all my password start with 13 am assuming that the salt equals to 13 so i tried translating it into java by using the java BCrypt library

    public boolean checkPassword(String passwordText, String DbHash) {
        boolean password_verified = false;
        if (null == DbHash || !DbHash.startsWith("$2a$")) {
            throw new java.lang.IllegalArgumentException("Invalid hash provided for comparison");
        }
        password_verified = BCrypt.checkpw(passwordText, DbHash);
        return (password_verified);
     }
    

    passwordText you actual password , DbHash stored hash

    This code check a password hash if password match or not

    there's a trick symfony hashed password start with $2y$ so to make this work you need to need change $2y$ to $2a$

    for exemple i have password with a hash value that is stored in my database

    String passwordText = "admin"; 
    String DbHash  = "$2y$13$VVmaKXzaS2QWgU1S4I8h5eJgC/DduF2fXmnhvcynro004GCUAQfr2";
    

    change this :

    String DbHash  = "$2y$13$VVmaKXzaS2QWgU1S4I8h5eJgC/DduF2fXmnhvcynro004GCUAQfr2"; 
    

    to this :

    String DbHash  = "$2a$13$VVmaKXzaS2QWgU1S4I8h5eJgC/DduF2fXmnhvcynro004GCUAQfr2";  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败