Package java.security

Examples of java.security.Signature$SignatureImpl


      if (getDomain() == null)
      {
         throw new SignatureException("You must have the domain attribute set on your signature header");
      }

      Signature signature = null;
      try
      {
         signature = Signature.getInstance(algorithm);
         signature.initSign(key);
      }
      catch (Exception e)
      {
         throw new SignatureException(e);
      }

      if (this.headers.size() > 0)
      {
         StringBuffer headerCat = new StringBuffer();
         int count = 0;
         for (int i = 0; i < this.headers.size(); i++)
         {
            String name = this.headers.get(i);
            if (i > 0) headerCat.append(":");
            headerCat.append(name);
         }
         attributes.put(HEADERS, headerCat.toString());
         updateSignatureWithHeader(headers, signature);
      }

      String encodedBodyHash = calculateEncodedHash(body, hashAlgorithm);

      attributes.put(BODY_HASH, encodedBodyHash);

      StringBuffer dosetaBuffer = new StringBuffer();

      boolean first = true;
      for (Map.Entry<String, String> entry : attributes.entrySet())
      {
         if (first) first = false;
         else dosetaBuffer.append(";");

         dosetaBuffer.append(entry.getKey()).append("=").append(entry.getValue());
      }
      if (!first) dosetaBuffer.append(";");
      dosetaBuffer.append("b=");
      String dosetaHeader = dosetaBuffer.toString();
      signature.update(dosetaHeader.getBytes());

      byte[] signed = signature.sign();
      setSignature(signed);
      String base64Signature = Base64.encodeBytes(signed);
      dosetaHeader += base64Signature;
//      System.out.println("***: " + dosetaHeader);
      this.headerValue = dosetaHeader;
View Full Code Here


      if (algorithm == null || !SigningAlgorithm.SHA256withRSA.getRfcNotation().toLowerCase().equals(algorithm.toLowerCase()))
      {
         throw new SignatureException("Unsupported algorithm " + algorithm);
      }

      Signature verifier = null;
      try
      {
         verifier = Signature.getInstance(SigningAlgorithm.SHA256withRSA.getJavaSecNotation());
         verifier.initVerify(key);
      }
      catch (Exception e)
      {
         throw new SignatureException(e);
      }


      String encodedBh = attributes.get("bh");
      if (encodedBh == null)
      {
         throw new SignatureException("There was no body hash (bh) in header");
      }

      byte[] bh = hash(body, SigningAlgorithm.SHA256withRSA.getJavaHashNotation());
      byte[] enclosedBh = null;
      try
      {
         enclosedBh = Base64.decode(encodedBh);
      }
      catch (IOException e)
      {
         throw new SignatureException("Failed to parse body hash (bh)", e);
      }

      if (Arrays.equals(bh, enclosedBh) == false)
      {
         throw new SignatureException("Body hashes do not match.");
      }
      updateSignatureWithHeader(headers, verifier);
      ParameterParser parser = new ParameterParser();
      String strippedHeader = parser.setAttribute(headerValue.toCharArray(), 0, headerValue.length(), ';', "b", "");
      verifier.update(strippedHeader.getBytes());
      if (verifier.verify(getSignature()) == false)
      {
         throw new SignatureException("Failed to verify signature.");
      }
   }
View Full Code Here

            String algo = SOSSignature.hashAlgorithm + "With"
                    + privKey.getAlgorithm();

            /* Signature */
            Signature sign = Signature.getInstance(algo, SOSSignature.provider);

            /* initialis. mit prived key */
            sign.initSign(privKey);

            /* lesen aus fileToSign und update() */
            FileInputStream fis = new FileInputStream(fileToSign);
            BufferedInputStream bufin = new BufferedInputStream(fis);
            byte[] buffer = new byte[1024];
            int len;
            while (bufin.available() != 0) {
                len = bufin.read(buffer);
                sign.update(buffer, 0, len);
            }
            bufin.close();

            /* generate unterschrift */
            byte[] realSig = sign.sign();

            /* save in to file �signature� */
            saveToFile(realSig, signFile);
        } catch (NoClassDefFoundError e) {
            throw new Exception("no such Definition : " + e);
View Full Code Here

            }

            /*
             * Signature Objekt erzeugen mit mit public key verify
             */
            Signature sig = Signature.getInstance(SOSSignature.hashAlgorithm
                    + "With" + pubKey.getAlgorithm(), SOSSignature.provider);
            sig.initVerify(pubKey);

            /* lesen from file data update() */
            FileInputStream datafis = new FileInputStream(dataFile);
            BufferedInputStream bufin = new BufferedInputStream(datafis);
            byte[] buffer = new byte[1024];
            int len;
            while (bufin.available() != 0) {
                len = bufin.read(buffer);
                sig.update(buffer, 0, len);
            }
            bufin.close();

            /* verify */
            boolean verifies = sig.verify(sigToVerify);
            //System.out.println("Signature verifies: " + verifies);

            return verifies;

        } catch (NoClassDefFoundError e) {
View Full Code Here

      System.out.println("No signature for " + filename);
      return false;
    }
    try {

      Signature sig = Signature.getInstance("SHA1withRSA");
      sig.initVerify(ks.getCertificate(ClientGameConfiguration.get("UPDATE_CERT_NAME")).getPublicKey());

      FileInputStream datafis = new FileInputStream(filename);
      InputStream buf = new BufferedInputStream(datafis);

      byte[] temp = new byte[1024];
      int length = 0;
      while (buf.available() != 0) {
        length = buf.read(temp);
        sig.update(temp, 0, length);
      }
      buf.close();

      boolean isVaild = sig.verify(hexStringToByteArray(signature));
      System.out.println("Validated " + filename + ": " + isVaild);
      return isVaild;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
View Full Code Here

        try {
            this.signatureAlgorithm.initVerify((PublicKey) publicKey);
        } catch (InvalidKeyException ex) {
            // reinstantiate Signature object to work around bug in JDK
            // see: http://bugs.sun.com/view_bug.do?bug_id=4953555
            Signature sig = this.signatureAlgorithm;
            try {
                this.signatureAlgorithm = Signature.getInstance(signatureAlgorithm.getAlgorithm());
            } catch (Exception e) {
                // this shouldn't occur, but if it does, restore previous
                // Signature
View Full Code Here

       
        // Verify the signature on SPKAC
        String signAlgString = PKCSObjectIdentifiers.md5WithRSAEncryption.equals(alg0) ? "MD5withRSA" :
                               PKCSObjectIdentifiers.md2WithRSAEncryption.equals(alg0) ? "MD2withRSA" :
                               PKCSObjectIdentifiers.sha1WithRSAEncryption.equals(alg0) ? "SHA1withRSA" : null;
        Signature signObj = Signature.getInstance(signAlgString);
        signObj.initVerify(pubKey);
        signObj.update(pkacSeq.getEncoded());
        boolean verified = signObj.verify(signature);
        if(!verified) throw new Exception("SignedPublicKeyAndChallenge verification failed.");
        map.put(CERT_REQ_PUBLICKEY, pkInfo);
        map.put(CERT_REQ_PUBLICKEY_OBJ, pubKey);
        if(((DERString)ch).getString() != null) map.put(PKAC_CHALLENGE, ((DERString)ch).getString());
        return map;
View Full Code Here

        catch (IOException e)
        {
            throw new IllegalArgumentException("can't encode public key");
        }

        Signature sig = null;

        try
        {
            if (provider == null) {
                sig = Signature.getInstance(sigAlgId.getObjectId().getId());
            }
            else {
                sig = Signature.getInstance(sigAlgId.getObjectId().getId(), provider);
            }
        }
        catch (NoSuchAlgorithmException e)
        {
            if (provider == null) {
                sig = Signature.getInstance(signatureAlgorithm);
            }
            else {
                sig = Signature.getInstance(signatureAlgorithm, provider);
            }
        }

        sig.initSign(signingKey);

        try
        {
            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
            DEROutputStream         dOut = new DEROutputStream(bOut);

            dOut.writeObject(reqInfo);

            sig.update(bOut.toByteArray());
        }
        catch (Exception e)
        {
            throw new SecurityException("exception encoding TBS cert request - " + e);
        }

        this.sigBits = new DERBitString(sig.sign());
    }
View Full Code Here

    public boolean verify(
        String provider)
        throws NoSuchAlgorithmException, NoSuchProviderException,
                InvalidKeyException, SignatureException
    {
        Signature   sig = null;

        try
        {
            if (provider == null) {
                sig = Signature.getInstance(sigAlgId.getObjectId().getId());
            }
            else {
                sig = Signature.getInstance(sigAlgId.getObjectId().getId(), provider);
            }
        }
        catch (NoSuchAlgorithmException e)
        {
            //
            // try an alternate
            //
            if (oids.get(sigAlgId.getObjectId().getId()) != null)
            {
                String  signatureAlgorithm = (String)oids.get(sigAlgId.getObjectId().getId());

                if (provider == null) {
                    sig = Signature.getInstance(signatureAlgorithm);
                }
                else {
                    sig = Signature.getInstance(signatureAlgorithm, provider);
                }
            }
        }

        sig.initVerify(this.getPublicKey(provider));

        try
        {
            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
            DEROutputStream         dOut = new DEROutputStream(bOut);

            dOut.writeObject(reqInfo);

            sig.update(bOut.toByteArray());
        }
        catch (Exception e)
        {
            throw new SecurityException("exception encoding TBS cert request - " + e);
        }

        return sig.verify(sigBits.getBytes());
    }
View Full Code Here

        PrivateKey      key,
        String          provider,
        SecureRandom    random)
        throws NoSuchProviderException, SecurityException, SignatureException, InvalidKeyException
    {
        Signature sig = null;

        try
        {
            if (provider == null) {
                sig = Signature.getInstance(sigOID.getId());
            }
            else {
                sig = Signature.getInstance(sigOID.getId(), provider);
            }
        }
        catch (NoSuchAlgorithmException ex)
        {
            try
            {
                if (provider == null) {
                    sig = Signature.getInstance(signatureAlgorithm);
                }
                else {
                    sig = Signature.getInstance(signatureAlgorithm, provider);
                }
            }
            catch (NoSuchAlgorithmException e)
            {
                throw new SecurityException("exception creating signature: " + e.toString());
            }
        }

        if (random != null)
        {
            sig.initSign(key, random);
        }
        else
        {
            sig.initSign(key);
        }

        TBSCertificateStructure tbsCert = tbsGen.generateTBSCertificate();

        try
        {
            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
            DEROutputStream         dOut = new DEROutputStream(bOut);

            dOut.writeObject(tbsCert);

            sig.update(bOut.toByteArray());
        }
        catch (Exception e)
        {
            throw new SecurityException("exception encoding TBS cert - " + e);
        }

        ASN1EncodableVector  v = new ASN1EncodableVector();

        v.add(tbsCert);
        v.add(sigAlgId);
        v.add(new DERBitString(sig.sign()));

        return new X509CertificateObject(new X509CertificateStructure(new DERSequence(v)));
    }
View Full Code Here

TOP

Related Classes of java.security.Signature$SignatureImpl

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.