Example usage for java.security Signature update

List of usage examples for java.security Signature update

Introduction

In this page you can find the example usage for java.security Signature update.

Prototype

public final void update(ByteBuffer data) throws SignatureException 

Source Link

Document

Updates the data to be signed or verified using the specified ByteBuffer.

Usage

From source file:com.streamsets.lib.security.util.DataSignature.java

public Verifier getVerifier(final PublicKey publicKey) {
    return new Verifier() {
        @Override//from  ww  w  .  j  av  a2s.c om
        public boolean verify(byte[] data, byte[] signature) throws GeneralSecurityException {
            Signature signer = Signature.getInstance("SHA1withDSA");
            signer.initVerify(publicKey);
            signer.update(data);
            return signer.verify(signature);
        }
    };
}

From source file:org.jgrades.security.utils.SignatureProvider.java

public byte[] sign(byte[] bytes) {
    try {//from   www.  ja va  2  s .  c o  m
        if (!Optional.ofNullable(bytes).isPresent()) {
            throw new IllegalArgumentException();
        }
        Signature signature = Signature.getInstance(SIGNATURE_PROVIDER_INTERFACE);
        signature.initSign(extractor.getPrivateKeyForSigning());
        signature.update(bytes);
        return signature.sign();
    } catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException e) {
        throw new CryptographyException(e);
    }
}

From source file:org.apache.james.jmap.crypto.JamesSignatureHandler.java

@Override
public String sign(String source) {
    Preconditions.checkNotNull(source);/*from w ww.j ava 2  s .  c  o  m*/
    try {
        Signature javaSignature = Signature.getInstance(ALGORITHM);
        javaSignature.initSign(privateKey);
        javaSignature.update(source.getBytes());
        return new Base64().encodeAsString(javaSignature.sign());
    } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.springframework.security.oauth.common.signature.TestRSA_SHA1SignatureMethod.java

/**
 * tests signing and verifying./* ww  w.  j a va  2s.c  o m*/
 */
public void testSignAndVerify() throws Exception {
    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
    generator.initialize(1024);
    KeyPair keyPair = generator.generateKeyPair();
    String baseString = "thisismysignaturebasestringthatshouldbemuchlongerthanthisbutitdoesnthavetobeandherearesomestrangecharacters!@#$%^&*)(*";

    byte[] signatureBytes;
    {
        Signature signer = Signature.getInstance("SHA1withRSA");
        signer.initSign(keyPair.getPrivate());
        signer.update(baseString.getBytes("UTF-8"));
        signatureBytes = signer.sign();
    }

    {
        Signature signer = Signature.getInstance("SHA1withRSA");
        signer.initVerify(keyPair.getPublic());
        signer.update(baseString.getBytes("UTF-8"));
        assertTrue(signer.verify(signatureBytes));
    }

    RSA_SHA1SignatureMethod signatureMethod = new RSA_SHA1SignatureMethod(keyPair.getPrivate(),
            keyPair.getPublic());
    String signature = signatureMethod.sign(baseString);
    signatureMethod.verify(baseString, signature);
}

From source file:org.apache.james.jmap.crypto.JamesSignatureHandler.java

@Override
public boolean verify(String source, String signature) {
    Preconditions.checkNotNull(source);/*w  w w .  ja va 2s  . c om*/
    Preconditions.checkNotNull(signature);
    try {
        Signature javaSignature = Signature.getInstance(ALGORITHM);
        javaSignature.initVerify(publicKey);
        javaSignature.update(source.getBytes());
        return javaSignature.verify(new Base64().decode(signature));
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw Throwables.propagate(e);
    } catch (SignatureException e) {
        LOGGER.warn("Attempt to use a malformed signature '" + signature + "' for source '" + source + "'", e);
        return false;
    }
}

From source file:org.diorite.impl.auth.properties.PropertyImpl.java

@Override
public boolean isSignatureValid(final PublicKey publicKey) {
    try {/* ww  w  .j a va2  s.  c om*/
        final Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initVerify(publicKey);
        signature.update(this.value.getBytes());
        return signature.verify(Base64.decodeBase64(this.signature));
    } catch (final NoSuchAlgorithmException | SignatureException | InvalidKeyException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:test.integ.be.fedict.hsm.PKCS11Test.java

@Test
public void testEToken() throws Exception {
    File tmpConfigFile = File.createTempFile("pkcs11-", ".conf");
    tmpConfigFile.deleteOnExit();/*  ww  w.j a v  a2  s. c o m*/
    PrintWriter configWriter = new PrintWriter(new FileOutputStream(tmpConfigFile));
    configWriter.println("name=test");
    configWriter.println("library=/usr/lib/libeTPkcs11.so");
    configWriter.println("slotListIndex=0");
    configWriter.close();
    SunPKCS11 sunPKCS11 = new SunPKCS11(tmpConfigFile.getAbsolutePath());
    Security.addProvider(sunPKCS11);

    Security.removeProvider(sunPKCS11.getName());
    sunPKCS11 = new SunPKCS11(tmpConfigFile.getAbsolutePath());
    Security.addProvider(sunPKCS11);

    Security.removeProvider(sunPKCS11.getName());
    sunPKCS11 = new SunPKCS11(tmpConfigFile.getAbsolutePath());
    Security.addProvider(sunPKCS11);

    Security.removeProvider(sunPKCS11.getName());
    sunPKCS11 = new SunPKCS11(tmpConfigFile.getAbsolutePath());
    Security.addProvider(sunPKCS11);

    Security.removeProvider(sunPKCS11.getName());
    sunPKCS11 = new SunPKCS11(tmpConfigFile.getAbsolutePath());
    Security.addProvider(sunPKCS11);

    KeyStore keyStore = KeyStore.getInstance("PKCS11", sunPKCS11);
    keyStore.load(null, "HSMProxy1234".toCharArray());
    Enumeration<String> aliasesEnum = keyStore.aliases();
    String alias = aliasesEnum.nextElement();

    PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, "HSMProxy1234".toCharArray());

    final int TEST_COUNT = 50;
    int count = TEST_COUNT;
    while (count > 0) {
        Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initSign(privateKey);
        signature.update("to be signed".getBytes());
        signature.sign();
        count--;
    }
}

From source file:com.ss.license.LicenseManager.java

/**
 * license/*  w  w w.  j a  va2  s.  c  o  m*/
 * Mac
 * 
 * 
 * @param license
 * @return
 * @throws Exception
 */
boolean validate(License license) throws Exception {
    String macAddress = license.getMacAddress();
    if (macAddress != null && macAddress.length() > 0) {
        String curMacAddress = "";
        if (!macAddress.equals(curMacAddress))
            return false;
    }
    String publicKey = FileHelper.readFile(new File(LicenseFactory.PUBLIC_KEY_FILE)).trim();

    X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(EasyUtils.decodeHex(publicKey));
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    java.security.PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);

    Signature sig = Signature.getInstance("DSA");
    sig.initVerify(pubKey);
    sig.update(license.getFingerprint());
    return sig.verify(EasyUtils.decodeHex(license.getLicenseSignature()));
}

From source file:com.ddubyat.develop.jhawtcode.util.PropertyUtil.java

private boolean validLicense(String email, String licenseCode) throws Exception {

    Resource res = applicationContext.getResource("classpath:jhc-public.der");
    InputStream is = res.getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    byte[] buffer = new byte[4096];
    byte[] pkey;/*from   ww w . j a v  a2 s . c o m*/
    int stream;
    while ((stream = is.read(buffer, 0, buffer.length)) != -1) {
        baos.write(buffer, 0, stream);
    }

    pkey = baos.toByteArray();

    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pkey);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PublicKey mypk = keyFactory.generatePublic(keySpec);

    Signature instance = Signature.getInstance("SHA1withRSA");
    instance.initVerify(mypk);
    instance.update(email.getBytes());

    //BASE64Decoder decoder = new BASE64Decoder();
    //byte[] decodedBytes = decoder.decodeBuffer(licenseCode);

    return instance.verify(DatatypeConverter.parseBase64Binary(licenseCode));
}

From source file:net.padlocksoftware.padlock.license.LicenseSigner.java

/**
 * Sign the supplied license using the PrivateKey given at the LicenseSigner instantation.
 * //from w  w w. j a  v a 2 s. c  o m
 * @param license The license to sign. Once signed, a license cannot be modified.
 * @throws IllegalStateException If the license has already been signed.
 */
public void sign(License license) {
    synchronized (license) {
        if (license.isSigned()) {
            throw new IllegalStateException("License is already signed");
        }

        String licenseString = ((LicenseImpl) license).concatenate();
        try {
            Signature signature = Signature.getInstance("SHA1withDSA");
            signature.initSign(privateKey);
            signature.update(licenseString.getBytes("UTF-8"));
            byte[] sig = signature.sign();
            String sigString = new String(Hex.encodeHex(sig));
            ((LicenseImpl) license).props.setProperty("signature", sigString);
        } catch (SignatureException ex) {
            logger.log(Level.SEVERE, "Signature failure.  Please verify a DSA security provider is available.",
                    ex);
        } catch (InvalidKeyException ex) {
            logger.log(Level.SEVERE, "Key failure.  Please verify a DSA security provider is available.", ex);
        } catch (NoSuchAlgorithmException ex) {
            logger.log(Level.SEVERE,
                    "Signature creation failure.  Please verify a DSA security provider is available.", ex);
        } catch (UnsupportedEncodingException ex) {
            logger.log(Level.SEVERE, "Encoding Exception.  Please report to support@padlocksoftware.net", ex);
        }
    }
}