安卓SHA512例子 [英] android sha512 example

查看:679
本文介绍了安卓SHA512例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 有人可以提供有关如何使用散列密码的Java / Android的一个例子 PW_HASH_ITERATION_COUNT SHA512迭代+盐?

  1. Can someone provide an example for java/android on how to hash a password using PW_HASH_ITERATION_COUNT iterations of sha512 + salt?

在伪code:

hash = sha512(concat(pw,salt));
for (i = 1; i<PW_HASH_ITERATION_COUNT; i++){
    hash = sha512(concat(hash,concat(pw,salt)));
}

其中, Z = CONCAT(X,Y)为x和y的连接。

也许使用消息摘要

你有什么建议为 PW_HASH_ITERATION_COUNT ?多少次迭代将是最大的,这样对一些老设备(2.1 +)

What would you suggest as PW_HASH_ITERATION_COUNT? How many iterations would be the maximum so that this might even run on some older devices (2.1+)

更新来更新更新

由于很好的理由,我们将使用 bcrypt 加密我们的密码。我们使用 jBCrypt 实施。

Due to good reasons, we will use bcrypt to encrypt our passwords. We use the jBCrypt implementation.

反正..回答的问题......这是$ C $下上面的问题使用SHA-512的消息摘要:

Anyway.. to answer the question... this is the code for the question above to use SHA-512 with the MessageDigest:

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import de.seduceme.utils.Base64;

public class PwStorage {
    public static int PW_HASH_ITERATION_COUNT = 5000;
    private static MessageDigest md;

    public static void main(String[] args) {
        String pw = "teüöäßÖst1";
        String salt = "e33ptcbnto8wo8c4o48kwws0g8ksck0";

        try {
            md = MessageDigest.getInstance("SHA-512");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new RuntimeException("No Such Algorithm");
        }

        String result = PwStorage.hashPw(pw, salt);
        System.out.println(result);
        // result: 2SzT+ikuO9FBq7KJWulZy2uZYujLjFkSpcOwlfBhi6VvajJMr6gxuRo5WvilrMlcM/44u2q8Y1smUlidZQrLCQ==
    }


    private static String hashPw(String pw, String salt) {
        byte[] bSalt;
        byte[] bPw;

        try {
            bSalt = salt.getBytes("UTF-8");
            bPw = pw.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Unsupported Encoding", e);
        }

        byte[] digest = run(bPw, bSalt);
        for (int i = 0; i < PW_HASH_ITERATION_COUNT - 1; i++) {
            digest = run(digest, bSalt);
        }

        return Base64.encodeBytes(digest);
    }

    private static byte[] run(byte[] input, byte[] salt) {
        md.update(input);
        return md.digest(salt);
    }
}

使用这Base64的LIB

推荐答案

读<一href="http://programmers.stackexchange.com/questions/51403/what-should-web-programmers-know-about-cryptography/51528#51528">my帖子这里,尤其是后我联系到关于<一个href="http://chargen.matasano.com/chargen/2007/9/7/enough-with-the-rainbow-tables-what-you-need-to-know-about-s.html"相对=nofollow>密码散列。

Read my post here, especially the post I linked to about password hashing.

  • 您最好应使用bcrypt或scrypt,而不是做你自己的密码哈希。
  • 但是,如果你要,你应该跑了几千次迭代最小,preferably更多。

是的,你可以使用消息摘要的SHA-512。每次调用摘要,该对象的状态自动复位,这是非常方便---你可以开始更新为下一次迭代,立竿见影。

Yes, you can use MessageDigest for SHA-512. Each time you call digest, the state of the object automatically resets, which is really handy---you can start updating for the next iteration straight away.

不过我还是认为你应该使用bcrypt或scrypt代替。为了你自己好,你的用户的不错。 : - )

But I still think you should use bcrypt or scrypt instead. For your own good, and the good of your users. :-)

这篇关于安卓SHA512例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆