private String digestToBase64(byte[] digest, int len) {
    CharBuffer cb = CharBuffer.allocate();

    Base64.encode(cb, digest, 0, len);

    return cb.close();
  }
  private Path writeTempFile(Node node) throws IOException {
    Path workDir = CauchoSystem.getWorkPath().lookup("_xsl");
    workDir.mkdirs();

    // Path temp = workDir.createTempFile("tmp", "xsl");

    WriteStream os = Vfs.lookup("null:").openWrite();
    Crc64Stream crcStream = new Crc64Stream(os.getSource());
    os.init(crcStream);
    try {
      XmlPrinter printer = new XmlPrinter(os);

      printer.printNode(node);
    } finally {
      os.close();
    }

    long crc = crcStream.getCRC();
    CharBuffer cb = new CharBuffer();
    Base64.encode(cb, crc);

    String crcValue = cb.toString().replace('/', '-');

    Path xslPath = workDir.lookup(crcValue + ".xsl");

    // temp.renameTo(xslPath);

    return xslPath;
  }
  /**
   * Creates a pseudo-random session id. If there's an old id and the group matches, then use it
   * because different applications on the same matchine should use the same cookie.
   */
  public String createSessionId(Env env) {
    String id;

    do {
      CharBuffer sb = new CharBuffer();

      Base64.encode(sb, RandomUtil.getRandomLong());
      Base64.encode(sb, env.getCurrentTime());

      id = sb.toString();
    } while (getSession(env, id, 0) != null);

    if (id == null || id.equals("")) throw new RuntimeException();

    return id;
  }
Example #4
0
  public static String encrypt(String toEncrypt) {
    try {
      Cipher cipher = Cipher.getInstance("AES");
      cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
      byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());
      return Base64.encode(encrypted);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return null;
  }
Example #5
0
  public static String decrypt(String encrypted) {
    try {
      Cipher cipher = Cipher.getInstance("AES");
      cipher.init(Cipher.DECRYPT_MODE, skeySpec);
      byte[] decodedBytes = Base64.decodeToByteArray(encrypted);
      byte[] original = cipher.doFinal(decodedBytes);
      return new String(original);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return null;
  }
Example #6
0
  public String write(Object in) throws IOException, JAXBException {
    Source src = (Source) in;

    try {
      ByteArrayOutputStream out = new ByteArrayOutputStream();

      _transformer.transform(src, new StreamResult(out));

      return Base64.encodeFromByteArray(out.toByteArray());
    } catch (TransformerException e) {
      if (src instanceof StreamSource) {
        StreamSource ss = (StreamSource) src;

        InputStream is = ss.getInputStream();

        if (is != null) {
          ByteArrayOutputStream out = new ByteArrayOutputStream();

          for (int ch = is.read(); ch >= 0; ch = is.read()) out.write(ch);

          return Base64.encodeFromByteArray(out.toByteArray());
        }

        Reader reader = ss.getReader();

        if (reader != null) {
          CharArrayWriter out = new CharArrayWriter();

          for (int ch = reader.read(); ch >= 0; ch = reader.read()) out.write(ch);

          return Base64.encode(new String(out.toCharArray()));
        }
      }

      throw new JAXBException(e);
    }
  }
Example #7
0
  public String sign(String uid, String nonce, String password) {
    try {
      MessageDigest digest = MessageDigest.getInstance("SHA-256");

      if (uid != null) digest.update(uid.getBytes("UTF-8"));

      digest.update(nonce.getBytes("UTF-8"));

      if (password != null) digest.update(password.getBytes("UTF-8"));

      return Base64.encode(digest.digest());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
Example #8
0
  protected Object read(String in) {
    byte[] bytes = Base64.decodeToByteArray(in);
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);

    return new StreamSource(bais);
  }