public static byte[] publicKeyToAddress(String string) { byte[] publicKeyBytes = null; try { publicKeyBytes = org.apache.commons.codec.binary.Hex.decodeHex(string.toCharArray()); } catch (DecoderException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // System.out.println(Utils.toHex(publicKeyBytes)); // System.out.println(publicKeyBytes.length); byte[] out = new byte[20]; byte[] hash = new byte[256]; byte[] hash2 = new byte[256]; try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); hash = digest.digest(publicKeyBytes); hash2 = digest.digest(hash); RIPEMD160Digest digest160 = new RIPEMD160Digest(); digest160.update(hash, 0, hash.length); digest160.doFinal(out, 0); } catch (Exception e) { // TODO: handle exception } byte[] ripemd_bytes = null; byte[] checksum = new byte[4]; try { ripemd_bytes = org.apache.commons.codec.binary.Hex.decodeHex( ("00" + Utils.toHex(out).toUpperCase()).toCharArray()); } catch (DecoderException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); hash = digest.digest(ripemd_bytes); hash2 = digest.digest(hash); } catch (Exception e) { // TODO: handle exception } System.arraycopy(hash2, 0, checksum, 0, checksum.length); byte[] combined = new byte[1 + out.length + checksum.length]; for (int i = 0; i < combined.length; ++i) { combined[i] = i < ripemd_bytes.length ? ripemd_bytes[i] : checksum[i - ripemd_bytes.length]; } // System.out.println(Utils.toHex(combined)); return (combined); }
public static byte[] privateKeyToWif(String string, boolean testnet) { string = testnet ? "EF" + string : "80" + string; byte[] privateKeyBytes = new byte[string.length()]; byte[] checksum = new byte[4]; try { privateKeyBytes = org.apache.commons.codec.binary.Hex.decodeHex(string.toCharArray()); } catch (DecoderException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } byte[] hash = new byte[256]; byte[] hash2 = new byte[256]; try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); hash = digest.digest(privateKeyBytes); hash2 = digest.digest(hash); } catch (Exception e) { // TODO: handle exception } System.arraycopy(hash2, 0, checksum, 0, checksum.length); byte[] combined = new byte[privateKeyBytes.length + checksum.length]; for (int i = 0; i < combined.length; ++i) { combined[i] = i < privateKeyBytes.length ? privateKeyBytes[i] : checksum[i - privateKeyBytes.length]; } return (combined); }
public static void jdkHMacMD5() { KeyGenerator keyGenerator; try { // 生成密钥 // keyGenerator = KeyGenerator.getInstance("HmacMD5"); // SecretKey secretKey = keyGenerator.generateKey(); // byte[] key = secretKey.getEncoded(); byte[] key = Hex.decodeHex(new char[] {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'}); // 还原密钥 SecretKey restoreSecretKey = new SecretKeySpec(key, "HmacMD5"); // 实例和初始化Mac Mac mac = Mac.getInstance(restoreSecretKey.getAlgorithm()); mac.init(restoreSecretKey); // 执行摘要 byte[] hmacMD5Bytes = mac.doFinal(src.getBytes()); System.out.println("jdk hmacMD5:" + Hex.encodeHexString(hmacMD5Bytes)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (DecoderException e) { e.printStackTrace(); } }
@IPSJexlMethod( description = "Returns a Map of Feed parameters from a Jquery formatted feed. For example a post of url: 'http://rss.news.yahoo.com/rss/yahoonewsroom', targetFolder: '//Sites/EnterpriseInvestments/News'", params = {@IPSJexlParam(name = "params", description = "The feed parameters")}) public Map<String, String> getFeedParameters(String params) throws IllegalArgumentException, FeedException, IOException { log.debug(params); URLCodec decode = new URLCodec(); HashMap<String, String> map = new HashMap<String, String>(); String[] opts = params.split("&"); String[] param; for (String s : opts) { param = s.split("="); try { map.put(decode.decode(param[0].trim()), decode.decode(param[1].trim())); } catch (DecoderException e) { log.debug(params); // TODO Auto-generated catch block e.printStackTrace(); } } return map; }
/** * Decodes a URL safe string into its original form. Escaped characters are converted back to * their original representation. * * @param pString URL safe string to convert into its original form * @return original string * @throws RuntimeException Thrown if URL decoding is unsuccessful. This only happens in the case * of a UnsupportedEncodingException which should never occur in reality. */ public static String decodeURL(String url) { try { return urlCodec.decode(url); } catch (DecoderException exp) { // NLogger.error( URLCodecUtils.class, exp, exp ); throw new RuntimeException(exp.toString() + ": " + exp.getMessage()); } }
public static byte[] hexDecode(String s) { try { return Hex.decodeHex(s.toCharArray()); } catch (DecoderException e) { throw new ClientProblemException( "Hex content is not valid hex: " + e.getMessage() + ": " + s); } }
private double[] getFeatureVectorFromArgs(TridentTuple queryTuple) { String args = queryTuple.getStringByField(EnsembleLearnerTopologyBuilder.drpcQueryArgsField.get(0)); try { return MlStormFeatureVectorUtils.deserializeToFeatureVector(args); } catch (DecoderException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new RuntimeException(e); } }
@Test public void scryptRuby() { int N = 1024; int r = 8; int p = 1; String hashed = SCryptUtil.scryptRuby(passwd, N, r, p); String[] parts = hashed.split("\\$"); assertEquals(5, parts.length); try { assertEquals(8, Hex.decodeHex(parts[3].toCharArray()).length); assertEquals(32, Hex.decodeHex(parts[4].toCharArray()).length); } catch (DecoderException e) { fail("There was an exception decoding the hashed value: \n" + e.getMessage()); } assertEquals(N, Integer.parseInt(parts[0], 16)); assertEquals(r, Integer.parseInt(parts[1], 16)); assertEquals(p, Integer.parseInt(parts[2], 16)); }
public static byte[] privateKeyToPublicKey(String private_key, boolean compressed) { byte[] privateKeyBytes = null; try { privateKeyBytes = org.apache.commons.codec.binary.Hex.decodeHex(private_key.toCharArray()); } catch (DecoderException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } X9ECParameters ecp = SECNamedCurves.getByName("secp256k1"); ECDomainParameters domainParams = new ECDomainParameters(ecp.getCurve(), ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed()); ECPrivateKeyParameters privateKey = new ECPrivateKeyParameters(new BigInteger(1, privateKeyBytes), domainParams); System.out.println(privateKey.getD()); byte[] publicKeyBIBytes = privateKey.getD().toByteArray(); ECPoint Q = domainParams.getG().multiply(new BigInteger(publicKeyBIBytes)); byte[] publicKeyBytes = Q.getEncoded(compressed); return publicKeyBytes; }
private void start() { byte[] key = new byte[16]; new Random().nextBytes(key); SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES"); String toEncrypt = "1"; try { Cipher encodingCipher = Cipher.getInstance("AES"); encodingCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encodedBytes = encodingCipher.doFinal(toEncrypt.getBytes(Charsets.UTF_8)); System.out.println("Encoded size: " + encodedBytes.length); String encodedHexString = new String(Hex.encodeHex(encodedBytes)); System.out.println("Encoded hex: " + encodedHexString); Cipher decodingCipher = Cipher.getInstance("AES"); decodingCipher.init(Cipher.DECRYPT_MODE, secretKeySpec); ByteBuffer wrap = ByteBuffer.wrap(decodingCipher.doFinal(Hex.decodeHex(encodedHexString.toCharArray()))); String result = new String(wrap.array(), Charsets.UTF_8); System.out.println(result); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (DecoderException e) { e.printStackTrace(); } }
public void map(Object key, Text value, Context context) throws IOException, InterruptedException { String inputStr = value.toString(); String patternStr = "(.+)\\s(\\d{2})\\:(.+)\\:\\s(.+?)\\s(.+?)\\s(.+?)\\s(.+)"; Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.find(); if (matchFound == true) { String s = matcher.group(4).toString() + matcher.group(5).toString(); byte[] bytes = null; try { bytes = Hex.decodeHex(s.toCharArray()); } catch (DecoderException e) { // TODO Auto-generated catch block e.printStackTrace(); } // System.out.println(Integer.toHexString(hash(bytes, 1))+"***-**/-"); Text wordK = new Text(); wordK.set(Integer.toHexString(hash(bytes, 1))); Text wordV = new Text(); String outdata = ""; String swapdata = ""; for (int i = 2; i <= 7; i++) { // String groupStr = matcher.group(i+3); switch (i) { case 2: swapdata = matcher.group(2).toString(); // online time at this version case 3: // wordV.set(matcher.group(3).toString()); //group(3) was mean MAC at before version // context.write(wordK, wordV); // outdata=outdata+matcher.group(3).toString(); // System.out.println(Integer.toHexString(hash(bytes, 1))+ ":" + matcher.group(i)); break; case 4: /*long t2 = Long.parseLong(matcher.group(4).toString()); //group(4) was mean online time at before version, so need to transform linuX timestamp java.util.Date time=new java.util.Date(t2*1000); //now MAC String inputStrhour = time.toString(); String patternStrhour = "(.+)\\s(.+)\\s(.+)\\s(.+)\\:(.+)\\:(.+)\\s(.+)"; Pattern patternhour = Pattern.compile(patternStrhour); Matcher matcherhour = patternhour.matcher(inputStrhour); matcherhour.find(); //wordV.set(matcherhour.group(4).toString()); //context.write(wordK, wordV); outdata=outdata+" "+matcherhour.group(4).toString(); //System.out.println(Integer.toHexString(hash(bytes, 1))+ ":" + matcher.group(i));*/ outdata = outdata + matcher.group(4).toString(); outdata = outdata + " " + swapdata; break; case 5: // System.out.println(a+ ":" + matcher.group(i)+"X"); break; case 6: int duringtime = Integer.valueOf(matcher.group(7).toString()); if (duringtime < 60) outdata = outdata + " A"; // wordV.set("A"); else if (60 <= duringtime && duringtime <= 360) outdata = outdata + " B"; // wordV.set("B"); else if (360 < duringtime && duringtime < 600) outdata = outdata + " C"; else if (600 <= duringtime && duringtime < 1800) outdata = outdata + " D"; // wordV.set("C"); else outdata = outdata + " E"; // wordV.set("D"); // System.out.println(Integer.toHexString(hash(bytes, 1))+ ":" + matcher.group(i)); break; } } wordV.set(outdata); context.write(wordK, wordV); } else { System.out.println("error"); } }
private void process(HttpServletRequest request, HttpServletResponse response) { // get current user Map<String, String> user = SessionManagement.getUser(request.getCookies()); // get parameters String verb = request.getParameter("verb"); String imgId = request.getParameter("imgId"); String retUrl = request.getParameter("retURL"); // get image metadata (if exists) Map<String, String> imageMeta = null; if (imgId != null && !imgId.equals("")) { imageMeta = ImageManagement.getImgMetadata(imgId); } if (verb != null) { // delete option if (verb.equals("delete")) { boolean success = false; boolean permitted = true; // user valid and image exists if (user != null && imageMeta != null) { // check user rights if (user.get("userId") != null && imageMeta.get("owner") != null && user.get("userId").equals(imageMeta.get("owner"))) { success = ImageManagement.deleteImage(imgId); } else { permitted = false; } } // output message to screen try { if (success) { response.setContentType("text/html"); PrintWriter out; out = response.getWriter(); out.print("<html><body><form action=\"imageServlet?verb=return"); if (retUrl != null && !retUrl.equals("")) { out.print("&retURL=" + retUrl); } out.print("\" method=\"post\">"); out.print("This image evolution has been deleted.<br/><br/>"); out.print("<input type=\"submit\" value=\"Continue\" />"); out.print("</form></body></html>"); } else if (!permitted) { response.setContentType("text/html"); PrintWriter out; out = response.getWriter(); out.print("<html><body><form action=\"imageServlet?verb=return"); if (retUrl != null && !retUrl.equals("")) { out.print("&retURL=" + retUrl); } out.print("\" method=\"post\">"); out.print( "You do not have permission to delete this image. Please contact image owner.<br/><br/>"); out.print("<input type=\"submit\" value=\"Continue\" />"); out.print("</form></body></html>"); } else { response.setContentType("text/html"); PrintWriter out; out = response.getWriter(); out.print("<html><body><form action=\"imageServlet?verb=return"); if (retUrl != null && !retUrl.equals("")) { out.print("&retURL=" + retUrl); } out.print("\" method=\"post\">"); out.print("There was an error while deleting this image.<br/><br/>"); out.print("<input type=\"submit\" value=\"Continue\" />"); out.print("</form></body></html>"); } } catch (IOException e) { e.printStackTrace(); } } // return (redirect) option else if (verb.equals("return")) { // redirect to desired page (default is dashboard) if (retUrl != null && !retUrl.equals("")) { try { response.sendRedirect((new URLCodec()).decode(retUrl)); } catch (IOException e) { e.printStackTrace(); } catch (DecoderException e) { e.printStackTrace(); } } else { try { response.sendRedirect("dashboard.jsp"); } catch (IOException e) { e.printStackTrace(); } } } // else verb not recognized, do nothing } // else no verb, do nothing }