Skip to content

Commit 34b6380

Browse files
committed
updating encode and hash in snippet
1 parent b5e89b9 commit 34b6380

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

webrisk/src/main/java/webrisk/SearchHashes.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,16 @@ public static void main(String[] args) throws IOException, NoSuchAlgorithmExcept
3939
// A hash prefix, consisting of the most significant 4-32 bytes of a SHA256 hash.
4040
// For JSON requests, this field is base64-encoded. Note that if this parameter is provided
4141
// by a URI, it must be encoded using the web safe base64 variant (RFC 4648).
42+
String uri = "http://example.com";
43+
String encodedUri = Base64.getUrlEncoder().encodeToString(uri.getBytes(StandardCharsets.UTF_8));
4244
MessageDigest digest = MessageDigest.getInstance("SHA-256");
43-
byte[] hash = digest.digest("https://example.com".getBytes(StandardCharsets.UTF_8));
44-
// Get the most significant 32 bytes.
45-
String hashPrefix = Base64.getUrlEncoder().encodeToString(hash).substring(0, 32);
46-
ByteString encodedHashPrefix = ByteString.copyFrom(hashPrefix.getBytes(StandardCharsets.UTF_8));
45+
byte[] encodedHashPrefix = digest.digest(encodedUri.getBytes(StandardCharsets.UTF_8));
4746

4847
// The ThreatLists to search in. Multiple ThreatLists may be specified.
4948
// For the list on threat types, see: https://cloud.google.com/web-risk/docs/reference/rpc/google.cloud.webrisk.v1#threattype
5049
List<ThreatType> threatTypes = Arrays.asList(ThreatType.MALWARE, ThreatType.SOCIAL_ENGINEERING);
5150

52-
searchHash(encodedHashPrefix, threatTypes);
51+
searchHash(ByteString.copyFrom(encodedHashPrefix), threatTypes);
5352
}
5453

5554
// Gets the full hashes that match the requested hash prefix.

0 commit comments

Comments
 (0)