Android sha512 示例

发布于 2024-11-15 19:00:00 字数 2536 浏览 5 评论 0 原文

  1. 有人可以提供一个关于如何使用 sha512 + salt 的 PW_HASH_ITERATION_COUNT 迭代来哈希密码的 java/android 示例吗?

    伪代码:

    hash = sha512(concat(pw,salt));
    for (i = 1; i

    其中 z = concat(x,y) 是 x 和 y 的串联。

    也许使用MessageDigest

  2. 您建议PW_HASH_ITERATION_COUNT是什么?最多需要多少次迭代才能在某些较旧的设备(2.1+)上运行

UPDATE UPDATE UPDATE

由于充分的理由,我们将使用 bcrypt 来加密我们的密码。我们使用 jBCrypt 实现。

无论如何..回答这个问题...这是上面问题的代码,将 SHA-512 与 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);
    }
}

使用 this Base64 库

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

    in pseudo code:

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

    Where z = concat(x,y) is the concatenation of x and y.

    Maybe using MessageDigest ?

  2. 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+)

UPDATE UPDATE UPDATE

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

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);
    }
}

With this Base64 lib.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫尐诺 2024-11-22 19:00:00

阅读我的帖子,尤其是帖子我链接到关于密码哈希

  • 理想情况下,您应该使用 bcrypt 或 scrypt,而不是自己进行密码散列。
  • 但如果必须的话,您应该至少运行几千次迭代,最好更多。

是的,您可以将 MessageDigest 用于 SHA-512。每次调用digest时,对象的状态都会自动重置,这非常方便——您可以立即开始更新下一次迭代。

但我仍然认为你应该使用 bcrypt 或 scrypt 代替。为了您自己的利益,也为了您的用户的利益。 :-)

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

  • You should ideally use bcrypt or scrypt rather than doing your own password hashing.
  • But if you must, you should run for a few thousand iterations at the minimum, preferably more.

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.

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

狼性发作 2024-11-22 19:00:00

发现 HMAC 足以满足您想要做的事情,并且它只执行 2 次

迭代到

hash = sha512(concat(xor(salt,nonce2),sha512(concat(xor(salt,nonce1),pw)));

a HMAC is found to be sufficient for what you wanna do and it does only 2 iterations

it boils down to

hash = sha512(concat(xor(salt,nonce2),sha512(concat(xor(salt,nonce1),pw)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文