Search in sources :

Example 6 with BASE64Encoder

use of sun.misc.BASE64Encoder in project fc-java-sdk by aliyun.

the class Code method setDir.

public Code setDir(String dir) throws IOException {
    String tempZipPath = "/tmp/code" + UUID.randomUUID() + ".zip";
    ZipUtils.zipDir(new File(dir), tempZipPath);
    File file = new File(tempZipPath);
    byte[] buffer = new byte[file.length() > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) file.length()];
    FileInputStream fis = new FileInputStream(tempZipPath);
    fis.read(buffer);
    fis.close();
    this.zipFile = new BASE64Encoder().encode(buffer);
    new File(tempZipPath).delete();
    return this;
}
Also used : BASE64Encoder(sun.misc.BASE64Encoder) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 7 with BASE64Encoder

use of sun.misc.BASE64Encoder in project ignite by apache.

the class JettyRestProcessorSignedSelfTest method signature.

/** {@inheritDoc} */
@Override
protected String signature() throws Exception {
    long ts = U.currentTimeMillis();
    String s = ts + ":" + REST_SECRET_KEY;
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        BASE64Encoder enc = new BASE64Encoder();
        md.update(s.getBytes());
        String hash = enc.encode(md.digest());
        return ts + ":" + hash;
    } catch (NoSuchAlgorithmException e) {
        throw new Exception("Failed to create authentication signature.", e);
    }
}
Also used : BASE64Encoder(sun.misc.BASE64Encoder) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 8 with BASE64Encoder

use of sun.misc.BASE64Encoder in project ignite by apache.

the class GridRestProtocolAdapter method authenticate.

/**
     * Authenticates current request.
     * <p>
     * Token consists of 2 parts separated by semicolon:
     * <ol>
     *     <li>Timestamp (time in milliseconds)</li>
     *     <li>Base64 encoded SHA1 hash of {1}:{secretKey}</li>
     * </ol>
     *
     * @param tok Authentication token.
     * @return {@code true} if authentication info provided in request is correct.
     */
protected boolean authenticate(@Nullable String tok) {
    if (F.isEmpty(secretKey))
        return true;
    if (F.isEmpty(tok))
        return false;
    StringTokenizer st = new StringTokenizer(tok, ":");
    if (st.countTokens() != 2)
        return false;
    String ts = st.nextToken();
    String hash = st.nextToken();
    String s = ts + ':' + secretKey;
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        BASE64Encoder enc = new BASE64Encoder();
        md.update(s.getBytes(UTF_8));
        String compHash = enc.encode(md.digest());
        return hash.equalsIgnoreCase(compHash);
    } catch (NoSuchAlgorithmException e) {
        U.error(log, "Failed to check authentication signature.", e);
    }
    return false;
}
Also used : StringTokenizer(java.util.StringTokenizer) BASE64Encoder(sun.misc.BASE64Encoder) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 9 with BASE64Encoder

use of sun.misc.BASE64Encoder in project sakuli by ConSol.

the class ScreenshotDivConverter method extractScreenshotAsBase64.

protected String extractScreenshotAsBase64(Throwable exception) {
    if (exception instanceof SakuliExceptionWithScreenshot) {
        Path screenshotPath = ((SakuliExceptionWithScreenshot) exception).getScreenshot();
        if (screenshotPath != null) {
            try {
                byte[] binaryScreenshot = Files.readAllBytes(screenshotPath);
                String base64String = new BASE64Encoder().encode(binaryScreenshot);
                for (String newLine : Arrays.asList("\n", "\r")) {
                    base64String = StringUtils.remove(base64String, newLine);
                }
                return base64String;
            } catch (IOException e) {
                exceptionHandler.handleException(new SakuliForwarderException(e, String.format("error during the BASE64 encoding of the screenshot '%s'", screenshotPath.toString())));
            }
        }
    }
    return null;
}
Also used : Path(java.nio.file.Path) BASE64Encoder(sun.misc.BASE64Encoder) SakuliExceptionWithScreenshot(org.sakuli.exceptions.SakuliExceptionWithScreenshot) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException) IOException(java.io.IOException)

Example 10 with BASE64Encoder

use of sun.misc.BASE64Encoder in project bazel by bazelbuild.

the class SignedJarBuilder method writeSignatureFile.

/** Writes a .SF file with a digest to the manifest. */
private void writeSignatureFile(SignatureOutputStream out) throws IOException, GeneralSecurityException {
    Manifest sf = new Manifest();
    Attributes main = sf.getMainAttributes();
    main.putValue("Signature-Version", "1.0");
    main.putValue("Created-By", "1.0 (Android)");
    BASE64Encoder base64 = new BASE64Encoder();
    MessageDigest md = MessageDigest.getInstance(DIGEST_ALGORITHM);
    PrintStream print = new PrintStream(new DigestOutputStream(new ByteArrayOutputStream(), md), true, SdkConstants.UTF_8);
    // Digest of the entire manifest
    mManifest.write(print);
    print.flush();
    main.putValue(DIGEST_MANIFEST_ATTR, base64.encode(md.digest()));
    Map<String, Attributes> entries = mManifest.getEntries();
    for (Map.Entry<String, Attributes> entry : entries.entrySet()) {
        // Digest of the manifest stanza for this entry.
        print.print("Name: " + entry.getKey() + "\r\n");
        for (Map.Entry<Object, Object> att : entry.getValue().entrySet()) {
            print.print(att.getKey() + ": " + att.getValue() + "\r\n");
        }
        print.print("\r\n");
        print.flush();
        Attributes sfAttr = new Attributes();
        sfAttr.putValue(DIGEST_ATTR, base64.encode(md.digest()));
        sf.getEntries().put(entry.getKey(), sfAttr);
    }
    sf.write(out);
    // As a workaround, add an extra CRLF in this case.
    if ((out.size() % 1024) == 0) {
        out.write('\r');
        out.write('\n');
    }
}
Also used : PrintStream(java.io.PrintStream) BASE64Encoder(sun.misc.BASE64Encoder) Attributes(java.util.jar.Attributes) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest) DigestOutputStream(java.security.DigestOutputStream) MessageDigest(java.security.MessageDigest) Map(java.util.Map)

Aggregations

BASE64Encoder (sun.misc.BASE64Encoder)20 MessageDigest (java.security.MessageDigest)6 File (java.io.File)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 PrintStream (java.io.PrintStream)2 KeyPair (java.security.KeyPair)2 KeyPairGenerator (java.security.KeyPairGenerator)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 PublicKey (java.security.PublicKey)2 X509Certificate (java.security.cert.X509Certificate)2 Calendar (java.util.Calendar)2 Date (java.util.Date)2 Map (java.util.Map)2 Attributes (java.util.jar.Attributes)2 Manifest (java.util.jar.Manifest)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1