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

Amy*_*Amy 5 java soap web-services digest wsse

我正在尝试使用使用密码摘要模式的 Web 服务,并且我的 Java 应用程序中具有这些函数来生成随机数、创建日期和密码摘要。我无法克服身份验证失败错误,并且文档对于他们是否需要 SHA-1 还是 MD5 并不太清楚,因为它顺便提到了两者。我尝试使用 MD5 而不是 SHA-1,得到了相同的结果。我设法通过 SoapUI 上的测试让请求正常工作,但我不知道该应用程序如何生成摘要/随机数。任何帮助表示赞赏。

这是我用来生成随机数和密码摘要的代码:

    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);
    }
Run Code Online (Sandbox Code Playgroud)

F. *_*n Q 3

解决方案是将行替换sha1.update(nonce.getBytes("UTF-8"));sha1.update(Base64.decodeBase64(nonce));