user1333856
user1333856

Reputation: 47

Sign a hash with CoSign SOAP API

I have an sha256 hash and my goal is to sign it with CoSign SOAP API. Could you provide me a Java example? The PDF Signing example is working like a charm, but it's not perfectly clear for me, how should I set the InputDocument for this task.

Thanks a lot!

Upvotes: 0

Views: 228

Answers (1)

Almog G.
Almog G.

Reputation: 817

The code example below demonstrates how to sign a hash value using CoSign Signature SOAP API. However, I would strongly recommend to use CoSign Signature API instead, which is richer in its functionality and is way more intuitive.

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

Upvotes: 1

Related Questions