/**
  * Preparation for signing, ie. encode <code>tbs</code> - To Be Signed and generate <code>nonce
  * </code>. The returned string should be the name of a pki client and should mapped to a view
  * with the same name.
  *
  * @param signData data used during signing
  * @return the name of the pki client
  * @throws SignatureException if preparation fails
  */
 public String prepareSign(SignatureData signData) throws SignatureException {
   encodeTbs(signData);
   if (!signData.getPkiClient().equals(PkiClient.MOBILE_BANKID)) {
     String nonce = signatureService.generateNonce(signData.getPkiClient());
     signData.setNonce(nonce);
   }
   return signData.getPkiClient().toString();
 }
  private SignatureData createSignatureDataFromXmlDigSig(String signature)
      throws SignatureException {

    // Start with setting some fields that are known directly
    SignatureData signatureData = new SignatureData();
    signatureData.setClientType(new ELegType("test", "test", PkiClient.NEXUS_PERSONAL_4X));
    signatureData.setSignature(new String(Base64.encode(signature.getBytes())));

    // For the rest we need to parse the XML
    try {
      Document document = createDocument(new ByteArrayInputStream(signature.getBytes()), false);

      // nonce and tbs
      XPath xpath = XPathFactory.newInstance().newXPath();
      XPathExpression expression =
          xpath.compile("/Signature/Object/bankIdSignedData/srvInfo/nonce/text()");
      String encodedNonce = (String) expression.evaluate(document, XPathConstants.STRING);
      String decodedNonce =
          new String(Base64.decode(encodedNonce)); // will be re-encoded again before it is sent
      signatureData.setNonce(decodedNonce);

      expression = xpath.compile("/Signature/Object/bankIdSignedData/usrVisibleData/text()");
      String encodedTbs = (String) expression.evaluate(document, XPathConstants.STRING);
      String decodedTbs =
          new String(Base64.decode(encodedTbs)); // this will be re-encoded again before it is sent

      signatureData.setEncodedTbs(encodedTbs);
      signatureData.setTbs(decodedTbs);

    } catch (XPathExpressionException e) {
      throw new SignatureException(e);
    } catch (ParserConfigurationException e) {
      throw new SignatureException(e);
    } catch (SAXException e) {
      throw new SignatureException(e);
    } catch (IOException e) {
      throw new SignatureException(e);
    }

    return signatureData;
  }