0

我有一个 sha256 哈希,我的目标是使用 CoSign SOAP API 对其进行签名。你能给我一个Java例子吗?PDF 签名示例的工作原理很吸引人,但对我来说还不是很清楚,我应该如何为这个任务设置 InputDocument。

非常感谢!

4

1 回答 1

1

下面的代码示例演示了如何使用 CoSign Signature SOAP API 对哈希值进行签名。但是,我强烈建议改用CoSign Signature API,它的功能更丰富,更直观。

public static byte[] SAPIWS_SignHash(byte[] data, String username, String domain, String password) throws Exception
{
    byte[] signedHash = null;

    try {

        // Set hash method & data
        DocumentHash documentHash = new DocumentHash();
        DigestMethodType digestMethod = new DigestMethodType();
        digestMethod.setAlgorithm("http://www.w3.org/2001/04/xmlenc#sha256");
        documentHash.setDigestMethod(digestMethod);
        documentHash.setDigestValue(data);

        // Set user credentials 
        ClaimedIdentity claimedIdentity = new ClaimedIdentity();
        NameIdentifierType nameIdentifier = new NameIdentifierType();
        nameIdentifier.setValue(username);
        nameIdentifier.setNameQualifier(domain);
        CoSignAuthDataType coSignAuthData = new CoSignAuthDataType();
        coSignAuthData.setLogonPassword(password);
        claimedIdentity.setName(nameIdentifier);
        claimedIdentity.setSupportingInfo(coSignAuthData);

        // Build complete request object
        SignRequest signRequest = new SignRequest();
        RequestBaseType.InputDocuments inputDocs = new RequestBaseType.InputDocuments();
        inputDocs.getOtherOrTransformedDataOrDocument().add(documentHash);
        RequestBaseType.OptionalInputs optionalParams = new RequestBaseType.OptionalInputs();
        optionalParams.setSignatureType("urn:ietf:rfc:2437:RSASSA-PKCS1-v1_5");
        optionalParams.setClaimedIdentity(claimedIdentity);
        signRequest.setOptionalInputs(optionalParams);
        signRequest.setInputDocuments(inputDocs);

        // Initiate service client
        DSS client = new DSS(new URL("https://prime.cosigntrial.com:8080/sapiws/dss.asmx"));

        // Send the request
        DssSignResult response = client.getDSSSoap().dssSign(signRequest);

        // Check response output
        if ("urn:oasis:names:tc:dss:1.0:resultmajor:Success".equals(response.getResult().getResultMajor())) {
            // On success- return signature buffer
            ResponseBaseType.OptionalOutputs doc = response.getOptionalOutputs();
            signedHash = doc.getDocumentWithSignature().getDocument().getBase64Data().getValue();
        }
        else {
            throw new Exception(response.getResult().getResultMessage().getValue());
        }
    }
    catch (Exception e)
    {
        System.out.println("Error: " + e.getMessage());
        e.printStackTrace();
    }

    return signedHash;
}
于 2015-06-24T09:42:15.180 回答