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:Main.java

public static void main(String[] args) throws Exception {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(1024);/*w  w w  .j a  v a 2  s. c o m*/
    KeyPair keyPair = kpg.genKeyPair();

    byte[] data = "test".getBytes("UTF8");

    Signature sig = Signature.getInstance("MD5WithRSA");
    sig.initSign(keyPair.getPrivate());
    sig.update(data);
    byte[] signatureBytes = sig.sign();
    System.out.println("Singature:" + new BASE64Encoder().encode(signatureBytes));

    sig.initVerify(keyPair.getPublic());
    sig.update(data);

    System.out.println(sig.verify(signatureBytes));
}

From source file:com.threerings.getdown.tools.AppletParamSigner.java

public static void main(String[] args) {
    try {//from w  ww .j a v  a2 s  .c  om
        if (args.length != 7) {
            System.err
                    .println("AppletParamSigner keystore storepass alias keypass " + "appbase appname imgpath");
            System.exit(255);
        }

        String keystore = args[0];
        String storepass = args[1];
        String alias = args[2];
        String keypass = args[3];
        String appbase = args[4];
        String appname = args[5];
        String imgpath = args[6];
        String params = appbase + appname + imgpath;

        KeyStore store = KeyStore.getInstance("JKS");
        store.load(new BufferedInputStream(new FileInputStream(keystore)), storepass.toCharArray());
        PrivateKey key = (PrivateKey) store.getKey(alias, keypass.toCharArray());
        Signature sig = Signature.getInstance("SHA1withRSA");
        sig.initSign(key);
        sig.update(params.getBytes());
        String signed = new String(Base64.encodeBase64(sig.sign()));
        System.out.println("<param name=\"appbase\" value=\"" + appbase + "\" />");
        System.out.println("<param name=\"appname\" value=\"" + appname + "\" />");
        System.out.println("<param name=\"bgimage\" value=\"" + imgpath + "\" />");
        System.out.println("<param name=\"signature\" value=\"" + signed + "\" />");

    } catch (Exception e) {
        System.err.println("Failed to produce signature.");
        e.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "BC");

    keyGen.initialize(512, new SecureRandom());

    KeyPair keyPair = keyGen.generateKeyPair();
    Signature signature = Signature.getInstance("DSA", "BC");

    signature.initSign(keyPair.getPrivate(), new SecureRandom());
    byte[] message = "abc".getBytes();
    signature.update(message);

    byte[] sigBytes = signature.sign();
    signature.initVerify(keyPair.getPublic());
    signature.update(message);/*from   w w  w .j  a v a  2  s .  c  om*/
    System.out.println(signature.verify(sigBytes));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");

    keyGen.initialize(512, new SecureRandom());

    KeyPair keyPair = keyGen.generateKeyPair();
    Signature signature = Signature.getInstance("SHA1withRSA", "BC");

    signature.initSign(keyPair.getPrivate(), new SecureRandom());

    byte[] message = "abc".getBytes();
    signature.update(message);

    byte[] sigBytes = signature.sign();
    signature.initVerify(keyPair.getPublic());
    signature.update(message);//ww w. j ava  2 s  .co  m
    System.out.println(signature.verify(sigBytes));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");
    keyGen.initialize(512, new SecureRandom());
    KeyPair keyPair = keyGen.generateKeyPair();

    Signature signature = Signature.getInstance("SHA256withRSA", "BC");
    signature.initSign(keyPair.getPrivate());

    byte[] message = "abc".getBytes();
    signature.update(message);

    byte[] sigBytes = signature.sign();
    Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
    cipher.init(Cipher.DECRYPT_MODE, keyPair.getPublic());

    byte[] decSig = cipher.doFinal(sigBytes);
    ASN1InputStream aIn = new ASN1InputStream(decSig);
    ASN1Sequence seq = (ASN1Sequence) aIn.readObject();

    System.out.println(ASN1Dump.dumpAsString(seq));

    MessageDigest hash = MessageDigest.getInstance("SHA-256", "BC");
    hash.update(message);//from   ww w  .  ja  v a2 s .c  o m

    ASN1OctetString sigHash = (ASN1OctetString) seq.getObjectAt(1);
    System.out.println(MessageDigest.isEqual(hash.digest(), sigHash.getOctets()));
}

From source file:Version2LicenseDecoder.java

public static void main(String[] args) throws IOException, Exception {
    new Version2LicenseDecoder();
    String lll = "AAABDA0ODAoPeNptUEtPg0AQvu+vIPG8ZsEKlmQPFda6DVAENB68rHTUbdotmQVi/71QTHykh5lM5nvMl7m4Q+2sOuOwwGHz8PomnDFnmVaOx9wrEoOtUTetPhi+ksXiJXREr3adGjckQjgNsWqBj3zKfMoCstWoLhNdg7EgNvqkFlkliryQpSA/DrzFDv7Qq2MDmdoDj9ZpKopILpIJV3Wre5gEu4n7BGhHE4+kSpsWjDI1iM9G4/FXomBMtMZ3ZbSdjm4PWpnN0M1knXX7V8D126MdDDl1SQnYA8qY31b5A5WRLKgfP0d0du8uSSkyPhRNPJ/5njcn38kHeiLjc8j5SHmH9Yey8P95XxVof60wKwITfDIxHZPgo323OEKd2FJ4BXvU7wIUIbLvXQNrkIAf4AL2Aeu4ZBRbTOA=X02dl";
    String lls = "AAABckRlc2NyaXB0aW9uPUpJUkE6IENvbW1lcmNpYWwKQ3JlYXRpb25EYXRlPTIwMTMtMDYtMDcKamlyYS5MaWNlbnNlRWRpdGlvbj1FTlRFUlBSSVNFCkV2YWx1YXRpb249ZmFsc2UKamlyYS5MaWNlbnNlVHlwZU5hbWU9Q09NTUVSQ0lBTApqaXJhLmFjdGl2ZT10cnVlCmxpY2Vuc2VWZXJzaW9uPTIKTWFpbnRlbmFuY2VFeHBpcnlEYXRlPTIwOTktMDYtMDcKT3JnYW5pc2F0aW9uPWpvaWFuZGpvaW4KU0VOPVNFTi1MMjYwNjIyOQpTZXJ2ZXJJRD1CVFBRLUlDSVItNkRYQy00SDFHCmppcmEuTnVtYmVyT2ZVc2Vycz0tMQpMaWNlbnNlSUQ9TElEU0VOLUwyNjA2MjI5CkxpY2Vuc2VFeHBpcnlEYXRlPTIwOTktMDYtMDcKUHVyY2hhc2VEYXRlPTIwMTMtMDYtMDc=X02g4";
    //      String sll = "Description=JIRA:Commercial\nCreationDate=2013-06-07\njira.LicenseEdition=ENTERPRISE\nEvaluation=false\njira.LicenseTypeName=COMMERCIAL\njira.active=true\nlicenseVersion=2\nMaintenanceExpiryDate=2099-06-07\nOrganisation=joiandjoin\nSEN=SEN-L2606229\nServerID=BTPQ-ICIR-6DXC-4H1G\njira.NumberOfUsers=-1\nLicenseID=LIDSEN-L2606229\nLicenseExpiryDate=2099-06-07\nPurchaseDate=2013-06-07";
    //      String sll = "com.allenta.jira.plugins.gitlab.gitlab-listener.enterprise=true\nDescription=GitLab Listener\\: Evaluation\nNumberOfUsers=-1\nCreationDate=2015-12-29\nContactEMail=xwturing@gmail.com\nEvaluation=true\ncom.allenta.jira.plugins.gitlab.gitlab-listener.Starter=false\nlicenseVersion=2\nMaintenanceExpiryDate=2099-01-27\nOrganisation=Evaluation license\nSEN=SEN-L7030895\ncom.allenta.jira.plugins.gitlab.gitlab-listener.active=true\nLicenseExpiryDate=2099-01-27\nLicenseTypeName=COMMERCIAL\nPurchaseDate=2015-12-29\n";
    //      String sll = "jira.product.jira-servicedesk.active=true\njira.product.jira-servicedesk.Starter=false\nNumberOfUsers=-1\nPurchaseDate=2016-01-05\ncom.atlassian.servicedesk.active=true\nLicenseTypeName=COMMERCIAL\nLicenseExpiryDate=2099-02-03\nContactEMail=xwturing@gmail.com\nServerID=BWGW-FKTG-N1UQ-PNHH\ncom.atlassian.servicedesk.LicenseTypeName=COMMERCIAL\njira.product.jira-servicedesk.NumberOfUsers=-1\nMaintenanceExpiryDate=2099-02-03\ncom.atlassian.servicedesk.enterprise=true\nLicenseID=LIDSEN-L7059162\nSEN=SEN-L7059162\nOrganisation=Evaluation license\nCreationDate=2016-01-05\ncom.atlassian.servicedesk.numRoleCount=-1\nlicenseVersion=2\nDescription=JIRA Service Desk (Server)\\: Evaluation\nEvaluation=true";
    String sll = "NumberOfUsers=-1\n" + "jira.product.jira-core.NumberOfUsers=-1\n" + "jira.NumberOfUsers=-1\n"
            + "PurchaseDate=2016-02-20\n" + "LicenseTypeName=COMMERCIAL\n" + "LicenseExpiryDate=2099-03-21\n"
            + "ContactEMail=xwturing@gmail.com\n" + "ServerID=BVDW-Q5Y0-CRXH-AI3I\n"
            + "jira.product.jira-core.Starter=false\n" + "jira.LicenseEdition=ENTERPRISE\n"
            + "jira.product.jira-core.active=true\n" + "MaintenanceExpiryDate=2099-03-21\n"
            + "LicenseID=LIDSEN-L7336401\n" + "SEN=SEN-L7336401\n" + "Organisation=Evaluation license\n"
            + "CreationDate=2016-02-20\n" + "licenseVersion=2\n"
            + "Description=JIRA Core (Server)\\: Evaluation\n" + "jira.active=true\n"
            + "jira.LicenseTypeName=COMMERCIAL\n" + "Evaluation=true";

    byte[] allData = sll.getBytes();
    Signature signature = Signature.getInstance("SHA1withDSA");
    signature.initVerify(PUBLIC_KEY);/*w  w  w . jav  a 2  s .co  m*/
    signature.update(allData);
    ByteArrayInputStream in = new ByteArrayInputStream(allData);
    DataInputStream dIn = new DataInputStream(in);
    int textLength = dIn.readInt();
    byte[] licenseText = new byte[textLength];
    dIn.read(licenseText);
    byte[] hash = new byte[dIn.available()];
    dIn.read(hash);
    String result = packLicense(allData, hash);
    System.out.println(result);
}

From source file:signature.SignLicense.java

public static void main(String[] args) throws Exception {

    if (args.length != 2) {
        System.out.println("Usage: SignLicense licenseFile privateKey");
        System.exit(1);/*from w w  w  .ja  v a2 s  . co m*/
    }

    Properties license = new Properties();
    license.load(new FileInputStream(args[0]));

    PrivateKey privateKey = Utils.readPrivateKeyFromFile(args[1]);
    Signature signature = Signature.getInstance("SHA1withDSA", "SUN");
    signature.initSign(privateKey);

    String[] propKeys = license.keySet().toArray(new String[0]);
    Arrays.sort(propKeys);
    for (String propKey : propKeys) {
        if (!"Signature".equals(propKey)) {
            String propValue = license.getProperty(propKey);
            signature.update(propValue.getBytes("UTF-8"));
        }
    }

    byte[] sig = signature.sign();
    license.setProperty("Signature", new String(Base64.encodeBase64(sig)));
    license.store(new FileOutputStream(args[0]), null);
}

From source file:SignatureTest.java

public static void main(String[] args) {
    try {//from   ww  w. j a  v a2  s .c o  m
        if (args[0].equals("-genkeypair")) {
            KeyPairGenerator pairgen = KeyPairGenerator.getInstance("DSA");
            SecureRandom random = new SecureRandom();
            pairgen.initialize(KEYSIZE, random);
            KeyPair keyPair = pairgen.generateKeyPair();
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));
            out.writeObject(keyPair.getPublic());
            out.close();
            out = new ObjectOutputStream(new FileOutputStream(args[2]));
            out.writeObject(keyPair.getPrivate());
            out.close();
        } else if (args[0].equals("-sign")) {
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
            PrivateKey privkey = (PrivateKey) keyIn.readObject();
            keyIn.close();

            Signature signalg = Signature.getInstance("DSA");
            signalg.initSign(privkey);

            File infile = new File(args[1]);
            InputStream in = new FileInputStream(infile);
            int length = (int) infile.length();
            byte[] message = new byte[length];
            in.read(message, 0, length);
            in.close();

            signalg.update(message);
            byte[] signature = signalg.sign();

            DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2]));
            int signlength = signature.length;
            out.writeInt(signlength);
            out.write(signature, 0, signlength);
            out.write(message, 0, length);
            out.close();
        } else if (args[0].equals("-verify")) {
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[2]));
            PublicKey pubkey = (PublicKey) keyIn.readObject();
            keyIn.close();

            Signature verifyalg = Signature.getInstance("DSA");
            verifyalg.initVerify(pubkey);

            File infile = new File(args[1]);
            DataInputStream in = new DataInputStream(new FileInputStream(infile));
            int signlength = in.readInt();
            byte[] signature = new byte[signlength];
            in.read(signature, 0, signlength);

            int length = (int) infile.length() - signlength - 4;
            byte[] message = new byte[length];
            in.read(message, 0, length);
            in.close();

            verifyalg.update(message);
            if (!verifyalg.verify(signature))
                System.out.print("not ");
            System.out.println("verified");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:testSig.java

public static void main(String[] args) {

    /* Test generating and verifying a DSA signature */

    try {//from w  w  w  . j  ava2 s  .co m

        /* generate a key pair */

        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
        keyGen.initialize(1024, new SecureRandom());
        KeyPair pair = keyGen.generateKeyPair();

        /*
         * create a Signature object to use for signing and verifying
         */

        Signature dsa = Signature.getInstance("SHA/DSA");

        /* initialize the Signature object for signing */

        PrivateKey priv = pair.getPrivate();

        dsa.initSign(priv);

        /* Update and sign the data */

        FileInputStream fis = new FileInputStream(args[0]);
        byte b;
        while (fis.available() != 0) {
            b = (byte) fis.read();
            dsa.update(b);
        }
        ;

        fis.close();

        /*
         * Now that all the data to be signed has been read in, sign it
         */
        byte[] sig = dsa.sign();

        /* Verify the signature */

        /* Initialize the Signature object for verification */
        PublicKey pub = pair.getPublic();
        dsa.initVerify(pub);

        /* Update and verify the data */

        fis = new FileInputStream(args[0]);
        while (fis.available() != 0) {
            b = (byte) fis.read();
            dsa.update(b);
        }
        ;

        fis.close();

        boolean verifies = dsa.verify(sig);

        System.out.println("signature verifies: " + verifies);

    } catch (Exception e) {
        System.err.println("Caught exception " + e.toString());
    }

}

From source file:MainClass.java

static void performVerification(String s, String alg, byte[] signature, PublicKey publicKey) throws Exception {
    Signature sign = Signature.getInstance(alg);
    sign.initVerify(publicKey);//from   www .j a  va  2  s.c  om
    sign.update(s.getBytes());
    System.out.println(sign.verify(signature));
}