List of usage examples for org.bouncycastle.util.encoders Base64 encode
public static byte[] encode(byte[] data)
From source file:ie.klatt.proxy.BinaryXml.java
private String readText(ByteBuffer content, int type) throws Exception { byte[] bytes; int dataSize; long word;/* w ww. j a v a2 s.com*/ String text; ByteBuffer byteBuffer; char letter; switch (type) { case 0x80: // ZeroText return "0"; case 0x82: // OneText return "1"; case 0x84: // FalseText return "false"; case 0x86: // TrueText return "true"; case 0x88: // Int8Text return String.valueOf((byte) (content.get() & 0xff)); case 0x8A: // Int16Text dataSize = 2; bytes = new byte[dataSize]; content.get(bytes); return String.valueOf(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getShort()); case 0x8C: // Int32Text dataSize = 4; bytes = new byte[dataSize]; content.get(bytes); return String.valueOf(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getInt()); case 0x8E: // Int64Text dataSize = 8; bytes = new byte[dataSize]; content.get(bytes); return String.valueOf(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getLong()); case 0x90: // FloatText dataSize = 4; bytes = new byte[dataSize]; content.get(bytes); return String.valueOf(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getFloat()); case 0x92: // DoubleText dataSize = 8; bytes = new byte[dataSize]; content.get(bytes); return String.valueOf(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getDouble()); case 0x94: // DecimalText dataSize = 16; bytes = new byte[dataSize]; content.get(bytes); return makeDecimal(bytes); case 0x96: // DateTimeText dataSize = 8; bytes = new byte[dataSize]; content.get(bytes); return makeDate(bytes); case 0x98: // Chars8Text dataSize = (content.get() & 0xff); bytes = new byte[dataSize]; content.get(bytes); return new String(bytes, "UTF-8"); case 0x9A: // Chars16Text dataSize = 2; bytes = new byte[dataSize]; content.get(bytes); word = (bytes[1] & 0xff) << 8 | (bytes[0] & 0xff); bytes = new byte[(int) word]; content.get(bytes); return new String(bytes, "UTF-8"); case 0x9C: // Chars32Text dataSize = 4; bytes = new byte[dataSize]; content.get(bytes); word = (bytes[3] & 0xff) << 24 | (bytes[2] & 0xff) << 16 | (bytes[1] & 0xff) << 8 | (bytes[0] & 0xff); bytes = new byte[(int) word]; content.get(bytes); return new String(bytes, "UTF-8"); case 0x9E: // Bytes8Text dataSize = (content.get() & 0xff); bytes = new byte[dataSize]; content.get(bytes); return new String(Base64.encode(bytes), "UTF-8"); case 0xA0: // Bytes16Text dataSize = 2; bytes = new byte[dataSize]; content.get(bytes); word = (bytes[1] & 0xff) << 8 | (bytes[0] & 0xff); bytes = new byte[(int) word]; content.get(bytes); return new String(Base64.encode(bytes), "UTF-8"); case 0xA2: // Bytes32Text dataSize = 4; bytes = new byte[dataSize]; content.get(bytes); word = (bytes[3] & 0xff) << 24 | (bytes[2] & 0xff) << 16 | (bytes[1] & 0xff) << 8 | (bytes[0] & 0xff); bytes = new byte[(int) word]; content.get(bytes); return new String(Base64.encode(bytes), "UTF-8"); case 0xA8: // EmptyText return ""; case 0xAA: // DictionaryText word = readInt(content); return dictionary.get(word); case 0xAC: // UniqueIdText dataSize = 16; bytes = new byte[dataSize]; content.get(bytes); return makeUrn(bytes); case 0xAE: // TimeSpanText dataSize = 8; bytes = new byte[dataSize]; content.get(bytes); return makeTimeSpan(bytes); case 0xB0: // UuidText dataSize = 16; bytes = new byte[dataSize]; content.get(bytes); return makeUuid(bytes); case 0xB2: // UInt64Text dataSize = 8; bytes = new byte[dataSize]; content.get(bytes); return makeUint64(bytes); case 0xB4: // BoolText return String.valueOf(content.get() != 0); case 0xB6: // UnicodeChars8Text dataSize = (content.get() & 0xff); bytes = new byte[dataSize]; content.get(bytes); text = ""; byteBuffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); while (byteBuffer.hasRemaining()) { text += byteBuffer.getChar(); } return text; case 0xB8:// UnicodeChars16Text dataSize = 2; bytes = new byte[dataSize]; content.get(bytes); word = (bytes[1] & 0xff) << 8 | (bytes[0] & 0xff); bytes = new byte[(int) word]; content.get(bytes); text = ""; byteBuffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); while (byteBuffer.hasRemaining()) { text += byteBuffer.getChar(); } return text; case 0xBA:// UnicodeChars32Text dataSize = 4; bytes = new byte[dataSize]; content.get(bytes); word = (bytes[3] & 0xff) << 24 | (bytes[2] & 0xff) << 16 | (bytes[1] & 0xff) << 8 | (bytes[0] & 0xff); bytes = new byte[(int) word]; content.get(bytes); text = ""; byteBuffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); while (byteBuffer.hasRemaining()) { text += byteBuffer.getChar(); } return text; case 0xBC: // QNameDictionaryText letter = (char) ('a' + (content.get())); text = letter + ":"; word = readInt(content); text += dictionary.get(word); return text; case 0xA4: // StartListText return new BinaryXml("").decode(content); } throw new Exception(Integer.toHexString(type)); }
From source file:ie.klatt.proxy.BinaryXml.java
private String decode(ByteBuffer content, String[] tag) throws Exception { content.order(ByteOrder.LITTLE_ENDIAN); Vector<String> stack = new Vector<String>(); if (tag != null) { stack.add(tag[0]);// ww w .ja v a2 s. c o m } while (content.remaining() > 0) { int RecordType = content.get() & 0xff, n; String text; long word; char letter; long length; byte[] bytes; ByteBuffer byteBuffer; switch (RecordType) { case 0x01: // EndElement if (tag != null) { tag[0] = stack.lastElement(); return xml; } checkBracket(true); xml += "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0x02: // Comment checkBracket(true); text = readString(content); xml += "<!-- " + text + " -->\n"; break; case 0x03: // Array String tagPointer[] = new String[1]; text = new BinaryXml().decode(content, tagPointer); RecordType = content.get() & 0xff; int dataSize = 0; switch (RecordType) { case 0xB5: // BoolTextWithEndElement dataSize = 1; break; case 0x8B: // Int16TextWithEndElement dataSize = 2; break; case 0x8D: // Int32TextWithEndElement dataSize = 4; break; case 0x8F: // Int64TextWithEndElement dataSize = 8; break; case 0x91: // FloatTextWithEndElement dataSize = 4; break; case 0x93: // DoubleTextWithEndElement dataSize = 8; break; case 0x95: // DecimalTextWithEndElement dataSize = 16; break; case 0x97: // DateTimeTextWithEndElement dataSize = 8; break; case 0xAF: // TimeSpanTextWithEndElement dataSize = 8; break; case 0XB1: // UuidTextWithEndElement dataSize = 16; break; } length = readInt(content); for (n = 0; n < length; n++) { bytes = new byte[dataSize + 1]; bytes[0] = (byte) (RecordType & 0xff); content.get(bytes, 1, bytes.length - 1); xml += new BinaryXml(text).decode(ByteBuffer.wrap(bytes), tagPointer); } break; case 0x04: // ShortAttribute text = readString(content); xml += " " + text + "=\"" + readText(content) + "\""; break; case 0x05: // Attribute xml += " " + readString(content) + ":" + readString(content) + "=\"" + readText(content) + "\""; break; case 0x06: // ShortDictionaryAttribute word = readInt(content); text = readText(content); xml += " " + dictionary.get(word) + "=\"" + text + "\""; break; case 0x07: // DictionaryAttribute xml += " " + readString(content) + ":" + readString(content) + "=\"" + readText(content) + "\""; break; case 0x08: // ShortXmlnsAttribute xml += " xmlns=\"" + readString(content) + "\""; break; case 0x09: // XmlnsAttribute text = readString(content); xml += " xmlns:" + text + "=\"" + readString(content) + "\""; break; case 0x0A: // ShortDictionaryXmlnsAttribute word = readInt(content); xml += " xmlns=\"" + dictionary.get(word) + "\""; break; case 0x0B: // DictionaryXmlnsAttribute text = readString(content); word = readInt(content); xml += " xmlns:" + text + "=\"" + dictionary.get(word) + "\""; break; case 0x0C: // PrefixDictionaryAttribute[A-Z] case 0x0D: case 0x0E: case 0x0F: case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17: case 0x18: case 0x19: case 0x1A: case 0x1B: case 0x1C: case 0x1D: case 0x1E: case 0x1F: case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: letter = (char) ('a' + (RecordType - 0x0C)); word = readInt(content); text = readText(content); xml += " " + letter + ":" + dictionary.get(word) + "=\"" + text + "\""; break; case 0x26: // PrefixDictionaryAttribute[A-Z] case 0x27: case 0x28: case 0x29: case 0x2A: case 0x2B: case 0x2C: case 0x2D: case 0x2E: case 0x2F: case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: case 0x3A: case 0x3B: case 0x3C: case 0x3D: case 0x3E: case 0x3F: letter = (char) ('a' + (RecordType - 0x26)); xml += " " + letter + ":" + readString(content) + "=\"" + readText(content) + "\""; break; case 0x40: // ShortElement text = readString(content); checkBracket(true); stack.add(text); xml += "<" + text; break; case 0x41: // Element text = readString(content) + ":" + readString(content); checkBracket(true); stack.add(text); xml += "<" + text; break; case 0x42: // ShortDictionaryElement word = readInt(content); text = dictionary.get(word); checkBracket(true); stack.add(text); xml += "<" + text; break; case 0x43: // DictionaryElement text = readString(content); word = readInt(content); text += ":" + dictionary.get(word); checkBracket(true); stack.add(text); xml += "<" + text; break; case 0x44: // PrefixDictionaryElement[A-Z] case 0x45: case 0x46: case 0x47: case 0x48: case 0x49: case 0x4A: case 0x4B: case 0x4C: case 0x4D: case 0x4E: case 0x4F: case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57: case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: letter = (char) ('a' + (RecordType - 0x44)); text = String.valueOf(letter); word = readInt(content); text += ":" + dictionary.get(word); checkBracket(true); stack.add(text); xml += "<" + text; break; case 0x5E: // PrefixElement[A-Z] case 0x5F: case 0x60: case 0x61: case 0x62: case 0x63: case 0x64: case 0x65: case 0x66: case 0x67: case 0x68: case 0x69: case 0x6a: case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f: case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77: letter = (char) ('a' + (RecordType - 0x5E)); text = letter + ":" + readString(content); checkBracket(true); stack.add(text); xml += "<" + text; break; case 0x81: // ZeroTextWithEndElement text = "0"; checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0x83: // OneTextWithEndElement text = "1"; checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0x85: // FalseTextWithEndElement text = "false"; checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0x87: // TrueTextWithEndElement text = "true"; checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0x89: // Int8TextWithEndElement text = String.valueOf((byte) (content.get() & 0xff)); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0x8B: // Int16TextWithEndElement dataSize = 2; bytes = new byte[dataSize]; content.get(bytes); text = String.valueOf(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getShort()); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0x8D: // Int32TextWithEndElement dataSize = 4; bytes = new byte[dataSize]; content.get(bytes); text = String.valueOf(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getInt()); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0x8F: // Int64TextWithEndElement dataSize = 8; bytes = new byte[dataSize]; content.get(bytes); text = String.valueOf(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getLong()); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0x91: // FloatTextWithEndElement dataSize = 4; bytes = new byte[dataSize]; content.get(bytes); text = String.valueOf(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getFloat()); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0x93: // DoubleTextWithEndElement dataSize = 8; bytes = new byte[dataSize]; content.get(bytes); text = String.valueOf(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getDouble()); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0x95: // DecimalTextWithEndElement dataSize = 16; bytes = new byte[dataSize]; content.get(bytes); text = makeDecimal(bytes); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0x97: // DateTimeTextWithEndElement dataSize = 8; bytes = new byte[dataSize]; content.get(bytes); text = makeDate(bytes); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0x99: // Chars8TextWithEndElement dataSize = (content.get() & 0xff); bytes = new byte[dataSize]; content.get(bytes); text = new String(bytes, "UTF-8"); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0x9B: // Chars16TextWithEndElement dataSize = 2; bytes = new byte[dataSize]; content.get(bytes); word = (bytes[1] & 0xff) << 8 | (bytes[0] & 0xff); bytes = new byte[(int) word]; content.get(bytes); text = new String(bytes, "UTF-8"); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0x9F: // Bytes8TextWithEndElement dataSize = (content.get() & 0xff); bytes = new byte[dataSize]; content.get(bytes); text = new String(Base64.encode(bytes), "UTF-8"); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0xA1: // Bytes16TextWithEndElement dataSize = 2; bytes = new byte[dataSize]; content.get(bytes); word = (bytes[1] & 0xff) << 8 | (bytes[0] & 0xff); bytes = new byte[(int) word]; content.get(bytes); text = new String(Base64.encode(bytes), "UTF-8"); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0xA3: // Bytes32TextWithEndElement dataSize = 4; bytes = new byte[dataSize]; content.get(bytes); word = (bytes[3] & 0xff) << 24 | (bytes[2] & 0xff) << 16 | (bytes[1] & 0xff) << 8 | (bytes[0] & 0xff); bytes = new byte[(int) word]; content.get(bytes); text = new String(Base64.encode(bytes), "UTF-8"); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0xA6: // EndListText listMode = false; return xml; case 0xA9: // EmptyTextWithEndElement checkBracket(); xml += "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0xAB: // DictionaryTextWithEndElement word = readInt(content); text = dictionary.get(word); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0XAD: // UniqueIdTextWithEndElement dataSize = 16; bytes = new byte[dataSize]; content.get(bytes); text = makeUrn(bytes); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0xAF: // TimeSpanTextWithEndElement dataSize = 8; bytes = new byte[dataSize]; content.get(bytes); text = makeTimeSpan(bytes); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0XB1: // UuidTextWithEndElement dataSize = 16; bytes = new byte[dataSize]; content.get(bytes); text = makeUuid(bytes); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0xB3: // UInt64TextWithEndElement dataSize = 8; bytes = new byte[dataSize]; content.get(bytes); text = makeUint64(bytes); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0xB5: // BoolTextWithEndElement text = String.valueOf((content.get() != 0)); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0xB7: // UnicodeChars8TextWithEndElement dataSize = (content.get() & 0xff); bytes = new byte[dataSize]; content.get(bytes); text = ""; byteBuffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); while (byteBuffer.hasRemaining()) { text += byteBuffer.getChar(); } checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0xB9: // UnicodeChars16TextWithEndElement dataSize = 2; bytes = new byte[dataSize]; content.get(bytes); word = (bytes[1] & 0xff) << 8 | (bytes[0] & 0xff); bytes = new byte[(int) word]; content.get(bytes); text = ""; byteBuffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); while (byteBuffer.hasRemaining()) { text += byteBuffer.getChar(); } checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0xBB: // UnicodeChars32TextWithEndElement dataSize = 4; bytes = new byte[dataSize]; content.get(bytes); word = (bytes[3] & 0xff) << 24 | (bytes[2] & 0xff) << 16 | (bytes[1] & 0xff) << 8 | (bytes[0] & 0xff); bytes = new byte[(int) word]; content.get(bytes); text = ""; byteBuffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); while (byteBuffer.hasRemaining()) { text += byteBuffer.getChar(); } checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; case 0xBD: // QNameDictionaryTextWithEndElement letter = (char) ('a' + (content.get())); text = letter + ":"; word = readInt(content); text += dictionary.get(word); checkBracket(); xml += text + "</" + stack.lastElement() + ">\n"; stack.remove(stack.lastElement()); break; default: checkBracket(); if (listMode && xml.length() > 0) { xml += " "; } xml += readText(content, RecordType); } if (isElement(RecordType)) { tagOpened = true; } } return xml; }
From source file:in.bbat.license.EncryptionManager.java
public static String encrypt(String paramString) { try {//from www. j av a2s . c om DESKeySpec localDESKeySpec = new DESKeySpec(password.getBytes("UTF8")); SecretKeyFactory localSecretKeyFactory = SecretKeyFactory.getInstance("DES"); SecretKey localSecretKey = localSecretKeyFactory.generateSecret(localDESKeySpec); byte[] arrayOfByte = paramString.getBytes("UTF8"); Cipher localCipher = Cipher.getInstance("DES"); localCipher.init(1, localSecretKey); String str = new String(Base64.encode(localCipher.doFinal(arrayOfByte)), "UTF-8"); return str; } catch (Exception localException) { LOG.error(localException); } return null; }
From source file:in.bbat.license.LicenseManager.java
private static void storeLicenseInfo(JSONObject registerResponse) throws JSONException, ActivationCodeValidationException, UnsupportedEncodingException { JSONObject localJSONObject = registerResponse.getJSONObject("license"); localJSONObject.put("instanceId", getInstanceId()); String license = localJSONObject.toString(); String encodedLicense = new String(Base64.encode(license.getBytes("UTF-8"))); System.setProperty("license.activation.enc", EncryptionManager.encrypt(encodedLicense)); }
From source file:in.gagan.common.util.CommonUtil.java
License:Apache License
public static Map<String, String> convertToHash(String passwordToSave) { // tuning parameters // these sizes are relatively arbitrary int seedBytes = 20; int hashBytes = 20; SecureRandom rng = null;//from w w w . ja va 2 s . c o m byte[] tmpSalt = null; PKCS5S2ParametersGenerator kdf = null; byte[] tmpHash = null; String hash = null; String salt = null; // increase iterations as high as your performance can tolerate // since this increases computational cost of password guessing // which should help security int iterations = 1000; Map<String, String> hashedMap = new HashMap<String, String>(); // to save a new password: try { rng = new SecureRandom(); tmpSalt = rng.generateSeed(seedBytes); kdf = new PKCS5S2ParametersGenerator(); kdf.init(passwordToSave.getBytes("UTF-8"), tmpSalt, iterations); tmpHash = ((KeyParameter) kdf.generateDerivedMacParameters(8 * hashBytes)).getKey(); hash = new String(Base64.encode(tmpHash)); salt = new String(Base64.encode(tmpSalt)); hashedMap.put(ApplicationConstants.SALTS, salt); hashedMap.put(ApplicationConstants.HASHES, hash); return hashedMap; } catch (Exception e) { logger.error("CommonUtil.convert hashing failed " + e); } finally { makeVariablesNull( new Object[] { tmpHash, hash, seedBytes, hashBytes, rng, tmpSalt, kdf, salt, iterations }); } return null; }
From source file:in.gagan.common.util.CommonUtil.java
License:Apache License
public static String convertToHashForPasswordAuthentication(String salt, String password) { PKCS5S2ParametersGenerator kdf = null; int hashBytes = 20; int iterations = 1000; byte[] tmpHash = null; String hash = null;//from w w w. j a v a 2s . c o m byte[] tmpSalt = null; try { tmpSalt = Base64.decode(salt); kdf = new PKCS5S2ParametersGenerator(); kdf.init(password.getBytes("UTF-8"), tmpSalt, iterations); tmpHash = ((KeyParameter) kdf.generateDerivedMacParameters(8 * hashBytes)).getKey(); hash = new String(Base64.encode(tmpHash)); return hash; } catch (Exception e) { logger.error("CommonUtils.convertToHashForPasswordAuthentication error: " + e); } finally { makeVariablesNull(new Object[] { tmpHash, hash, hashBytes, tmpSalt, kdf, salt, iterations, password }); } return null; }
From source file:infn.eToken.TokenUtils.java
License:Apache License
public static boolean createProxy(String filename, int keybit, Boolean rfc, int lifetime, String serialNumber, String smtp_host, String email, String sender_email, String expiration, String CN_label, String tokenConf, String tokenPIN) { boolean result = false; Provider provider = new sun.security.pkcs11.SunPKCS11(tokenConf); KeyPair_Cert tokenData = getTokenData(serialNumber, smtp_host, email, sender_email, expiration, tokenPIN, provider);/* w w w . j a v a2 s . c o m*/ if (tokenData != null) { Security.addProvider(provider); KeyPair_Cert reqData = createProxyCertificate(keybit, rfc, lifetime, CN_label, tokenData.getX509Cert(), tokenData.getPrivate()); if (reqData != null) { File proxyFile = new File(filename); org.bouncycastle.openssl.PEMWriter pemWriter = null; log.debug("Saving self-signed proxy certificate in " + filename); try { java.io.FileOutputStream fos = new FileOutputStream(proxyFile); pemWriter = new PEMWriter(new OutputStreamWriter(fos)); // Export the certificate request pemWriter.writeObject(reqData.getX509Cert()); // Export the Private Key used to sign the request pemWriter.writeObject(reqData.getPrivate()); // Export the User Certificate from the token byte[] encoding = tokenData.getX509Cert().getEncoded(); pemWriter.write("-----BEGIN CERTIFICATE-----\n"); char[] bufX = new char[64]; encoding = Base64.encode(encoding); for (int i = 0; i < encoding.length; i += bufX.length) { int index = 0; while (index != bufX.length) { if ((i + index) >= encoding.length) break; bufX[index] = (char) encoding[i + index]; index++; } pemWriter.write(bufX, 0, index); pemWriter.write("\n"); } pemWriter.write("-----END CERTIFICATE-----\n"); pemWriter.close(); log.info("Setting file permissions for [ " + proxyFile + " ]"); Process p = Runtime.getRuntime().exec("chmod 600 " + proxyFile); int cmdExitCode = p.waitFor(); if (cmdExitCode == 0) log.info("Setting file permissions [ DONE ] "); else log.info("Setting file permissions [ FAILED ] "); result = true; } catch (Exception e) { log.error(e.getMessage()); } /*finally { try { pemWriter.close(); } catch (IOException ex) { java.util.logging.Logger .getLogger(TokenUtils.class.getName()) .log(Level.SEVERE, null, ex); } }*/ } Security.removeProvider(provider.getName()); } return result; }
From source file:io.milton.http.http11.auth.OAuth2Helper.java
License:Apache License
public static String toState(String providerId, String returnUrl) { StringBuilder sb = new StringBuilder(providerId); if (returnUrl != null) { sb.append("||"); sb.append(returnUrl);/*ww w .j av a 2 s .c om*/ } byte[] arr = Base64.encode(sb.toString().getBytes()); String encoded = new String(arr); return encoded; }
From source file:it.trento.comune.j4sign.cms.utils.CMSBuilder.java
License:Open Source License
/** * Triggers encoded digest recalculation. * <p>//from w w w . j av a 2 s .co m * Invokes the private <code>getAuthenticatedAttributesBytes()</code> method * obtaining the raw digest, encapsulates it in a <code>digestInfo</code> * structure, finally encoding the result in <code>base64</code>. * </p> * * @return the <code>base64</code> encoding of the data to be signed. * @throws NoSuchAlgorithmException * @throws IOException */ public String updateEncodedDigest() throws NoSuchAlgorithmException, IOException { String ed = null; byte[] bytesToSign = getAuthenticatedAttributesBytes(); byte[] rawDigest = null; byte[] dInfoBytes = null; if (bytesToSign != null) { rawDigest = applyDigest(digestAlgorithm, bytesToSign); System.out.println("Raw digest bytes:\n" + formatAsString(rawDigest, " ", WRAP_AFTER)); System.out.println("Encapsulating in a DigestInfo..."); dInfoBytes = encapsulateInDigestInfo(digestAlgorithm, rawDigest); System.out.println("DigestInfo bytes:\n" + formatAsString(dInfoBytes, " ", WRAP_AFTER)); ed = new String(Base64.encode(dInfoBytes)); this.encodedDigest = ed; } return ed; }
From source file:it.trento.comune.j4sign.cms.utils.CMSBuilder.java
License:Open Source License
public String getEncodedGMTSigningTime() { if (this.signingTime != null) { SimpleDateFormat df = new SimpleDateFormat("dd MMMMM yyyy HH:mm:ss z"); Calendar cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT")); df.setCalendar(cal);/*from ww w .j av a 2s . c om*/ try { return new String(Base64.encode(df.format(this.signingTime).getBytes("UTF-8"))); } catch (UnsupportedEncodingException e) { System.out.println(e); } } return ""; }