加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡阳站长网 (https://www.0734zz.cn/)- 数据集成、设备管理、备份、数据加密、智能搜索!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

如何从数据库中存储和使用shiro的盐

发布时间:2021-05-25 17:25:14 所属栏目:MsSql教程 来源:网络整理
导读:我在申请认证时使用shiro.我使用散列密码和盐,我将它们存储在我的数据库中,如下所示: private User createUserWithHashedPassword(String inName,String inFirstName,String inLastName,String inPassword){ ByteSource salt = randomNumberGenerator.nextBy

我在申请认证时使用shiro.我使用散列密码和盐,我将它们存储在我的数据库中,如下所示:

private User createUserWithHashedPassword(String inName,String inFirstName,String inLastName,String inPassword){

    ByteSource salt  = randomNumberGenerator.nextBytes(32);

    byte[] byteTabSalt  = salt.getBytes();

    String strSalt = byteArrayToHexString(byteTabSalt);

    String hashedPasswordBase64 = new Sha256Hash(inPassword,salt,1024).toBase64();

    return new User(inName,inFirstName,inLastName,hashedPasswordBase64,strSalt);
}

我在我的数据库中使用String存储salt.现在在我的领域我想从数据库中获取我的数据,我使用了一个transactionnal服务.但是我的salt是一个Strong,所以我希望它使用静态方法返回ByteSource类型:

ByteSource byteSourceSalt = Util.bytes(salt); //where the salt is a String

但是当我创建我的SaltedAuthenticationInfo时,它不会授权.

我认为我的问题来自我的转换方法:

private String byteArrayToHexString(byte[] bArray){

        StringBuffer buffer = new StringBuffer();

        for(byte b : bArray) {
            buffer.append(Integer.toHexString(b));
            buffer.append(" ");
        }

 return buffer.toString().toUpperCase();    
}

谢谢你的帮助.

解决方法

正如在优秀的答案 https://stackoverflow.com/a/20206115/603901中所提到的,Shiro的DefaultPasswordService已经为每个密码生成了唯一的salt.

但是,不需要实现自定义PasswordService来向每个用户的salt添加私有盐(有时称为“pepper”).私有盐可以在shiro.ini中配置:

[main]
hashService = org.apache.shiro.crypto.hash.DefaultHashService
hashService.hashIterations = 500000
hashService.hashAlgorithmName = SHA-256
hashService.generatePublicSalt = true
# privateSalt needs to be base64-encoded in shiro.ini but not in the Java code
hashService.privateSalt = myVERYSECRETBase64EncodedSalt
passwordMatcher = org.apache.shiro.authc.credential.PasswordMatcher

passwordService = org.apache.shiro.authc.credential.DefaultPasswordService
passwordService.hashService = $hashService
passwordMatcher.passwordService = $passwordService

用于生成匹配密码哈希的Java代码:

DefaultHashService hashService = new DefaultHashService();
hashService.setHashIterations(HASH_ITERATIONS); // 500000
hashService.setHashAlgorithmName(Sha256Hash.ALGORITHM_NAME);
hashService.setPrivateSalt(new SimpleByteSource(PRIVATE_SALT)); // Same salt as in shiro.ini,but NOT base64-encoded.
hashService.setGeneratePublicSalt(true);

DefaultPasswordService passwordService = new DefaultPasswordService();
passwordService.setHashService(hashService);
String encryptedPassword = passwordService.encryptPassword("PasswordForThisUser");

生成的哈希看起来像这样:

$shiro1$SHA-256$500000$An4HRyqMJlZ58utACtyGDQ==$nKbIY9Nd9vC89G4SjdnDfka49mZiesjWgDsO/4Ly4Qs=

私有盐不存储在数据库中,如果攻击者获得对数据库转储的访问权限,则很难破解密码.

此示例是使用shiro-1.2.2创建的

感谢https://github.com/Multifarious/shiro-jdbi-realm/blob/master/src/test/resources/shiro.ini获取shiro.ini语法的帮助

(编辑:衡阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读