简体   繁体   English

需要帮助创建有效的随机数

[英]Need help creating a valid nonce

I am trying to consume a web service that uses Password Digest mode, and I have these functions in my Java application to generate a random nonce, creation date and password digest. 我正在尝试使用使用密码摘要模式的Web服务,并且我在Java应用程序中使用这些函数来生成随机的随机数,创建日期和密码摘要。 I can't get past the Authentication Failed error, and the documentation isn't overly clear on whether they want SHA-1 or MD5, as it mentions both in passing. 我无法通过身份验证失败错误,并且文档对于他们是否需要SHA-1或MD5并不过分清楚,因为它在传递中提到了两者。 I've tried MD5 instead of SHA-1 and I am getting the same result. 我尝试过MD5而不是SHA-1,我得到了相同的结果。 I managed to get the requests to work via a test on SoapUI, but I have no idea how that application is generating the digest / nonce. 我设法通过SoapUI上的测试获得工作请求,但我不知道该应用程序是如何生成摘要/ nonce的。 Any help is appreciated. 任何帮助表示赞赏。

Here's the code I am using to generate the nonce and the password digest: 这是我用来生成nonce和密码摘要的代码:

    private static SOAPMessage createSOAPRequest() throws Exception 
    {
        String password = "FakePassword";

        String nonce = generateNonce(); 
        System.out.println("Nonce = " + nonce);

        DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date today = Calendar.getInstance().getTime();
        String created = dateFormatter.format(today);
        System.out.println("Created = " + created);

        String passwordDigest = buildPasswordDigest(nonce, created, password);
        System.out.println("Password Digest = " + passwordDigest);
    }

    private static String buildPasswordDigest(String nonce, String created, String password) throws NoSuchAlgorithmException, UnsupportedEncodingException
    {
        MessageDigest sha1;
        String passwordDigest = null;

        try
        {
            sha1 = MessageDigest.getInstance("SHA-1");
            sha1.update(Base64.decodeBase64(nonce));
            sha1.update(created.getBytes("UTF-8"));
            passwordDigest = new String(Base64.encodeBase64(sha1.digest(password.getBytes("UTF-8"))));
            sha1.reset();
        }
        catch (NoSuchAlgorithmException e) 
        {
            e.printStackTrace();
        }

        return passwordDigest;
    }

    private static String generateNonce() throws NoSuchAlgorithmException, NoSuchProviderException, UnsupportedEncodingException
    {
        String dateTimeString = Long.toString(new Date().getTime());
        byte[] nonceByte = dateTimeString.getBytes();
        return Base64.encodeBase64String(nonceByte);
    }

The solution was to replace the line sha1.update(nonce.getBytes("UTF-8")); 解决方案是替换sha1.update(nonce.getBytes("UTF-8"));sha1.update(nonce.getBytes("UTF-8")); with sha1.update(Base64.decodeBase64(nonce)); with sha1.update(Base64.decodeBase64(nonce));

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

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