使用CoSign SOAP API签署哈希

时间:2015-06-23 14:40:09

标签: cosign-api

我有一个sha256哈希,我的目标是用CoSign SOAP API签名。你能给我一个Java例子吗? PDF签名示例就像魅力一样,但对我来说并不是很清楚,我应该如何为此任务设置InputDocument。

非常感谢!

1 个答案:

答案 0 :(得分: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;
}