Example usage for org.eclipse.jgit.lib Constants encode

List of usage examples for org.eclipse.jgit.lib Constants encode

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Constants encode.

Prototype

public static byte[] encode(String str) 

Source Link

Document

Convert a string to a byte array in the standard character encoding.

Usage

From source file:com.collabnet.gerrit.SecureStoreJasypt.java

License:Apache License

private static void saveSecure(final FileBasedConfig sec) throws IOException {
    if (FileUtil.modified(sec)) {
        final byte[] out = Constants.encode(sec.toText());
        final File path = sec.getFile();
        final LockFile lf = new LockFile(path, FS.DETECTED);
        if (!lf.lock()) {
            throw new IOException("Cannot lock " + path);
        }//from   w w  w.  ja  v a  2  s .  c om
        try {
            FileUtil.chmod(0600, new File(path.getParentFile(), path.getName() + ".lock"));
            lf.write(out);
            if (!lf.commit()) {
                throw new IOException("Cannot commit write to " + path);
            }
        } finally {
            lf.unlock();
        }
    }
}

From source file:com.gitblit.git.SideBandProgressMonitor.java

License:Eclipse Distribution License

private void send(StringBuilder s) {
    if (write) {//from w  ww . j a v a  2s  . c  o m
        try {
            out.write(Constants.encode(s.toString()));
            out.flush();
        } catch (IOException err) {
            write = false;
        }
    }
}

From source file:com.google.gerrit.common.FileUtil.java

License:Apache License

public static boolean modified(FileBasedConfig cfg) throws IOException {
    byte[] curVers;
    try {/*from   w  w w .ja v a2 s .c  om*/
        curVers = IO.readFully(cfg.getFile());
    } catch (FileNotFoundException notFound) {
        return true;
    }

    byte[] newVers = Constants.encode(cfg.toText());
    return !Arrays.equals(curVers, newVers);
}

From source file:com.google.gerrit.gpg.PushCertificateChecker.java

License:Apache License

private PGPSignature readSignature(PushCertificate cert) throws IOException {
    ArmoredInputStream in = new ArmoredInputStream(
            new ByteArrayInputStream(Constants.encode(cert.getSignature())));
    PGPObjectFactory factory = new BcPGPObjectFactory(in);
    Object obj;//from ww  w. j  av  a  2s  .  c om
    while ((obj = factory.nextObject()) != null) {
        if (obj instanceof PGPSignatureList) {
            PGPSignatureList sigs = (PGPSignatureList) obj;
            if (!sigs.isEmpty()) {
                return sigs.get(0);
            }
        }
    }
    return null;
}

From source file:com.google.gerrit.gpg.PushCertificateChecker.java

License:Apache License

private void checkSignature(PGPSignature sig, PushCertificate cert, PublicKeyStore store, List<String> problems)
        throws PGPException, IOException {
    PGPPublicKeyRingCollection keys = store.get(sig.getKeyID());
    if (!keys.getKeyRings().hasNext()) {
        problems.add("No public keys found for key ID " + keyIdToString(sig.getKeyID()));
        return;//w  ww .j a  v  a  2  s .co  m
    }
    PGPPublicKey signer = PublicKeyStore.getSigner(keys, sig, Constants.encode(cert.toText()));
    if (signer == null) {
        problems.add("Signature by " + keyIdToString(sig.getKeyID()) + " is not valid");
        return;
    }
    CheckResult result = publicKeyChecker.check(signer, store);
    if (!result.isOk()) {
        StringBuilder err = new StringBuilder("Invalid public key ").append(keyToString(signer)).append(":");
        for (String problem : result.getProblems()) {
            err.append("\n  ").append(problem);
        }
        problems.add(err.toString());
    }
}

From source file:com.google.gerrit.gpg.testutil.TestKey.java

License:Apache License

private static ArmoredInputStream newStream(String armored) throws IOException {
    return new ArmoredInputStream(new ByteArrayInputStream(Constants.encode(armored)));
}

From source file:com.google.gerrit.pgm.init.InitUtil.java

License:Apache License

static void saveSecure(final FileBasedConfig sec) throws IOException {
    if (modified(sec)) {
        final byte[] out = Constants.encode(sec.toText());
        final File path = sec.getFile();
        final LockFile lf = new LockFile(path, FS.DETECTED);
        if (!lf.lock()) {
            throw new IOException("Cannot lock " + path);
        }/*from   ww  w .  ja  v a2s . c  om*/
        try {
            chmod(0600, new File(path.getParentFile(), path.getName() + ".lock"));
            lf.write(out);
            if (!lf.commit()) {
                throw new IOException("Cannot commit write to " + path);
            }
        } finally {
            lf.unlock();
        }
    }
}

From source file:com.google.gerrit.pgm.init.InitUtil.java

License:Apache License

private static boolean modified(FileBasedConfig cfg) throws IOException {
    byte[] curVers;
    try {// w w  w . j  av a  2s .c om
        curVers = IO.readFully(cfg.getFile());
    } catch (FileNotFoundException notFound) {
        return true;
    }

    byte[] newVers = Constants.encode(cfg.toText());
    return !Arrays.equals(curVers, newVers);
}

From source file:com.google.gerrit.server.account.GroupUUID.java

License:Apache License

public static AccountGroup.UUID make(String groupName, PersonIdent creator) {
    MessageDigest md = Constants.newMessageDigest();
    md.update(Constants.encode("group " + groupName + "\n"));
    md.update(Constants.encode("creator " + creator.toExternalString() + "\n"));
    return new AccountGroup.UUID(ObjectId.fromRaw(md.digest()).name());
}

From source file:com.google.gerrit.server.git.gpg.PushCertificateChecker.java

License:Apache License

private void checkSignature(PGPSignature sig, PushCertificate cert, PGPPublicKeyRingCollection keys,
        List<String> problems) {
    List<String> deferredProblems = new ArrayList<>();
    boolean anyKeys = false;
    for (PGPPublicKeyRing kr : keys) {
        PGPPublicKey k = kr.getPublicKey();
        anyKeys = true;//from  ww w . java  2s  . c o m
        try {
            sig.init(new BcPGPContentVerifierBuilderProvider(), k);
            sig.update(Constants.encode(cert.toText()));
            if (!sig.verify()) {
                // TODO(dborowitz): Privacy issues with exposing fingerprint/user ID
                // of keys having the same ID as the pusher's key?
                deferredProblems.add("Signature not valid with public key: " + keyToString(k));
                continue;
            }
            CheckResult result = publicKeyChecker.check(k, sig.getKeyID());
            if (result.isOk()) {
                return;
            }
            StringBuilder err = new StringBuilder("Invalid public key (").append(keyToString(k)).append("):");
            for (int i = 0; i < result.getProblems().size(); i++) {
                err.append('\n').append("  ").append(result.getProblems().get(i));
            }
            problems.add(err.toString());
            return;
        } catch (PGPException e) {
            deferredProblems
                    .add("Error checking signature with public key (" + keyToString(k) + ": " + e.getMessage());
        }
    }
    if (!anyKeys) {
        problems.add("No public keys found for Key ID " + keyIdToString(sig.getKeyID()));
    } else {
        problems.addAll(deferredProblems);
    }
}