/** * Non-idempotent RuleKeys are normally output as strings of 'x' characters, but when comparing * two sets of RuleKeys in textual form it is necessary to mangle one of the two sets, so that * non-idempotent RuleKeys are never considered equal. */ public String toString(boolean mangleNonIdempotent) { if (!isIdempotent()) { return new String(new char[Hashing.sha1().bits() / 4]) .replace("\0", mangleNonIdempotent ? "y" : "x"); } return hashCode.toString(); }
private String generateHashKey(StringBuilder hashSourceBuilder, String apiKey) { String hashSource = hashSourceBuilder.toString() + apiKey; HashCode hashCode = Hashing.sha1().hashString(hashSource, Charset.defaultCharset()); Log.d( LOG_TAG, String.format("creating sha1 from [%s] resulted with hash [%s]", hashSource, hashCode)); return hashCode.toString(); }
private static String createStringToSign( String date, String credentialScope, HashCode hashedCanonicalRequest) { return ALGORITHM + "\n" + date + "\n" + credentialScope + "\n" + hashedCanonicalRequest.toString(); }
private static HashCode buildHashedCanonicalRequest( String method, String endpoint, HashCode hashedPayload, String canonicalizedHeadersString, String signedHeaders) { return Hashing.sha256() .newHasher() .putString(method, UTF_8) .putString("\n", UTF_8) .putString(endpoint, UTF_8) .putString("\n", UTF_8) .putString("\n", UTF_8) .putString(canonicalizedHeadersString, UTF_8) .putString("\n", UTF_8) .putString(signedHeaders, UTF_8) .putString("\n", UTF_8) .putString(hashedPayload.toString(), UTF_8) .hash(); }
/** * This test validates that the md5sum() method returns hashes that match the official test * vectors specified in RFC 1321, The MD5 Message-Digest Algorithm. * * @throws Exception */ @Test public void testValidateMd5Sum() throws Exception { ImmutableMap<String, String> testVectors = ImmutableMap.<String, String>builder() .put("", "d41d8cd98f00b204e9800998ecf8427e") .put("a", "0cc175b9c0f1b6a831c399e269772661") .put("abc", "900150983cd24fb0d6963f7d28e17f72") .put("message digest", "f96b697d7cb7938d525a2f31aaf161d0") .put("abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b") .put( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "d174ab98d277d9f5a5611c2c9f419d9f") .put( "12345678901234567890123456789012345678901234567890123456789012345678901234567890", "57edf4a22be3c955ac49da2e2107b67a") .build(); for (String testInput : testVectors.keySet()) { FileSystemUtils.writeContentAsLatin1(testFile, testInput); HashCode result = NativePosixFiles.md5sum(testFile.getPathString()); assertThat(testVectors).containsEntry(testInput, result.toString()); } }
private Builder setVal(@Nullable File file) { if (file != null) { // Compute a separate SHA-1 for the file contents and feed that into messageDigest rather // than the file contents, in order to avoid the overhead of escaping SEPARATOR in the file // content. try { byte[] fileBytes = Files.toByteArray(file); HashCode fileSha1 = Hashing.sha1().hashBytes(fileBytes); if (logElms != null) { logElms.add( String.format("file(path=\"%s\", sha1=%s):", file.getPath(), fileSha1.toString())); } feed(fileSha1.asBytes()); } catch (IOException e) { // The file is nonexistent/unreadable; generate a RuleKey that prevents accidental // caching. if (logElms != null) { logElms.add(String.format("file(path=\"%s\", sha1=random):", file.getPath())); } nonIdempotent(); } } return separate(); }
/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String loginName = request.getParameter("loginName"); String loginPassword = request.getParameter("loginPassword"); boolean valid = validation.Validation.validate(loginName, loginPassword); HttpSession session = request.getSession(); if (valid) { session.setAttribute("loginName", loginName); request.getSession().setMaxInactiveInterval(600); JSONObject json = new JSONObject().put("status", "ok"); HashFunction hf = Hashing.sha1(); // unique token !? Random rand = new Random(); int randomNum = (rand.nextInt(9999999 - 1) + 1) + 1; HashCode hc = hf.newHasher().putString(loginName, Charsets.UTF_8).putInt(randomNum).hash(); PlayerRequest.setTokenToPlayer(loginName, hc.toString()); json.put("token", hc.toString()); PrintWriter out = response.getWriter(); out.println(json); out.println( "<!DOCTYPE html PUBLIC " + " // W3C//DTD HTML 4.01" + " // Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\"" + "<html>" + " <head>" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">" + "<title>Transactions</title>" + " </head>" + " <body>" + "<form method=\"POST\" action=\"Deposit\">" + " <br>Deposit: <input type=\"text\" name=\"deposit\" />" + "<input type=\"hidden\" name=\"token\" value=\"" + hc.toString() + "\"/> " + "<input type=\"hidden\" name=\"loginName\" value=\"" + loginName + "\"/> " + "<input type=\"submit\" name=\"depositB\" value=\"Deposit\" />" + " </form>" + " <form method=\"POST\" action=\"Withdraw\">" + " <br> Withdraw: <input type=\"text\" name=\"withdraw\" />" + "<input type=\"hidden\" name=\"token\" value=\"" + hc.toString() + "\"/>" + "<input type=\"hidden\" name=\"loginName\" value=\"" + loginName + "\"/> " + " <input type=\"submit\" name=\"withdrawB\" value=\"Withdraw\" />" + "</form>" + "<form method=\"POST\" action = \"Logout\">" + "<br><input type=\"hidden\" name=\"token\" value=\"" + hc.toString() + "\"/>" + "<br><input type=\"hidden\" name=\"loginName\" value=\"" + loginName + "\"/>" + "<input type=\"submit\" name=\"logout\" value=\"Logout\" />" + "</form>" + " </body>" + "</html>"); } else { JSONObject json = new JSONObject().put("status", "invalid"); PrintWriter out = response.getWriter(); out.println(json); } }
/** * @param ipAddress * @param userAgent * @return */ public final String generateHash(final String ipAddress, final String userAgent) { HashCode hc = this.hasher.putString(ipAddress).putString(userAgent).hash(); return hc.toString(); }