搜索哈希
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
创建 WebRisk 客户端并搜索哈希
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[],[],null,["# Search a hash\n\nCreate a Webrisk client and search for a hash\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Using the Update API](/web-risk/docs/update-api)\n\nCode sample\n-----------\n\n### Java\n\n\nTo authenticate to Web Risk, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.webrisk.v1.https://cloud.google.com/java/docs/reference/google-cloud-webrisk/latest/com.google.cloud.webrisk.v1.WebRiskServiceClient.html;\n import com.google.protobuf.https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString.html;\n import com.google.webrisk.v1.https://cloud.google.com/java/docs/reference/google-cloud-webrisk/latest/com.google.webrisk.v1.SearchHashesRequest.html;\n import com.google.webrisk.v1.https://cloud.google.com/java/docs/reference/google-cloud-webrisk/latest/com.google.webrisk.v1.SearchHashesResponse.html;\n import com.google.webrisk.v1.https://cloud.google.com/java/docs/reference/google-cloud-webrisk/latest/com.google.webrisk.v1.SearchHashesResponse.html.https://cloud.google.com/java/docs/reference/google-cloud-webrisk/latest/com.google.webrisk.v1.SearchHashesResponse.ThreatHash.html;\n import com.google.webrisk.v1.https://cloud.google.com/java/docs/reference/google-cloud-webrisk/latest/com.google.webrisk.v1.ThreatType.html;\n import java.io.IOException;\n import java.nio.charset.StandardCharsets;\n import java.security.MessageDigest;\n import java.security.NoSuchAlgorithmException;\n import java.util.Arrays;\n import java.util.Base64;\n import java.util.List;\n\n public class SearchHashes {\n\n public static void main(String[] args) throws IOException, NoSuchAlgorithmException {\n // TODO(developer): Replace these variables before running the sample.\n // A hash prefix, consisting of the most significant 4-32 bytes of a SHA256 hash.\n // For JSON requests, this field is base64-encoded. Note that if this parameter is provided\n // by a URI, it must be encoded using the web safe base64 variant (RFC 4648).\n String uri = \"http://example.com\";\n String encodedUri = Base64.getUrlEncoder().encodeToString(uri.getBytes(StandardCharsets.UTF_8));\n MessageDigest digest = MessageDigest.getInstance(\"SHA-256\");\n byte[] encodedHashPrefix = digest.digest(encodedUri.getBytes(StandardCharsets.UTF_8));\n\n // The ThreatLists to search in. Multiple ThreatLists may be specified.\n // For the list on threat types, see: https://cloud.google.com/web-risk/docs/reference/rpc/google.cloud.webrisk.v1#threattype\n List\u003cThreatType\u003e threatTypes = Arrays.asList(https://cloud.google.com/java/docs/reference/google-cloud-webrisk/latest/com.google.webrisk.v1.ThreatType.html.MALWARE, https://cloud.google.com/java/docs/reference/google-cloud-webrisk/latest/com.google.webrisk.v1.ThreatType.html.SOCIAL_ENGINEERING);\n\n searchHash(https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString.html.https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString.html#com_google_protobuf_ByteString_copyFrom_byte___(encodedHashPrefix), threatTypes);\n }\n\n // Gets the full hashes that match the requested hash prefix.\n // This is used after a hash prefix is looked up in a threatList and there is a match.\n // The client side threatList only holds partial hashes so the client must query this method\n // to determine if there is a full hash match of a threat.\n public static void searchHash(https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ByteString.html encodedHashPrefix, List\u003cThreatType\u003e threatTypes)\n throws IOException {\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests. After completing all of your requests, call\n // the `webRiskServiceClient.close()` method on the client to safely\n // clean up any remaining background resources.\n try (https://cloud.google.com/java/docs/reference/google-cloud-webrisk/latest/com.google.cloud.webrisk.v1.WebRiskServiceClient.html webRiskServiceClient = https://cloud.google.com/java/docs/reference/google-cloud-webrisk/latest/com.google.cloud.webrisk.v1.WebRiskServiceClient.html.create()) {\n\n // Set the hashPrefix and the threat types to search in.\n https://cloud.google.com/java/docs/reference/google-cloud-webrisk/latest/com.google.webrisk.v1.SearchHashesResponse.html response = webRiskServiceClient.searchHashes(\n https://cloud.google.com/java/docs/reference/google-cloud-webrisk/latest/com.google.webrisk.v1.SearchHashesRequest.html.newBuilder()\n .https://cloud.google.com/java/docs/reference/google-cloud-webrisk/latest/com.google.webrisk.v1.SearchHashesRequest.Builder.html#com_google_webrisk_v1_SearchHashesRequest_Builder_setHashPrefix_com_google_protobuf_ByteString_(encodedHashPrefix)\n .addAllThreatTypes(threatTypes)\n .build());\n\n // Get all the hashes that match the prefix. Cache the returned hashes until the time\n // specified in threatHash.getExpireTime()\n // For more information on response type, see: https://cloud.google.com/web-risk/docs/reference/rpc/google.cloud.webrisk.v1#threathash\n for (https://cloud.google.com/java/docs/reference/google-cloud-webrisk/latest/com.google.webrisk.v1.SearchHashesResponse.ThreatHash.html threatHash : response.https://cloud.google.com/java/docs/reference/google-cloud-webrisk/latest/com.google.webrisk.v1.SearchHashesResponse.html#com_google_webrisk_v1_SearchHashesResponse_getThreatsList__()) {\n System.out.println(threatHash.getHash());\n }\n System.out.println(\"Completed searching threat hashes.\");\n }\n }\n }\n\n### Python\n\n\nTo authenticate to Web Risk, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/webrisk/latest/\n\n\n def search_hashes(hash_prefix: bytes, threat_type: https://cloud.google.com/python/docs/reference/webrisk/latest/.https://cloud.google.com/python/docs/reference/webrisk/latest/google.cloud.webrisk_v1.types.ThreatType.html) -\u003e list:\n \"\"\"Gets the full hashes that match the requested hash prefix.\n\n This is used after a hash prefix is looked up in a threatList and there is a match.\n The client side threatList only holds partial hashes so the client must query this method\n to determine if there is a full hash match of a threat.\n\n Args:\n hash_prefix: A hash prefix, consisting of the most significant 4-32 bytes of a SHA256 hash.\n For JSON requests, this field is base64-encoded. Note that if this parameter is provided\n by a URI, it must be encoded using the web safe base64 variant (RFC 4648).\n Example:\n uri = \"http://example.com\"\n sha256 = sha256()\n sha256.update(base64.urlsafe_b64encode(bytes(uri, \"utf-8\")))\n hex_string = sha256.digest()\n\n threat_type: The ThreatLists to search in. Multiple ThreatLists may be specified.\n For the list on threat types, see:\n https://cloud.google.com/web-risk/docs/reference/rpc/google.cloud.webrisk.v1#threattype\n threat_type = [webrisk_v1.ThreatType.MALWARE, webrisk_v1.ThreatType.SOCIAL_ENGINEERING]\n\n Returns:\n A hash list that contain all hashes that matches the given hash prefix.\n \"\"\"\n webrisk_client = https://cloud.google.com/python/docs/reference/webrisk/latest/.https://cloud.google.com/python/docs/reference/webrisk/latest/google.cloud.webrisk_v1.services.web_risk_service.WebRiskServiceClient.html()\n\n # Set the hashPrefix and the threat types to search in.\n request = https://cloud.google.com/python/docs/reference/webrisk/latest/.https://cloud.google.com/python/docs/reference/webrisk/latest/google.cloud.webrisk_v1.types.SearchHashesRequest.html()\n request.hash_prefix = hash_prefix\n request.threat_types = [threat_type]\n\n response = webrisk_client.https://cloud.google.com/python/docs/reference/webrisk/latest/google.cloud.webrisk_v1.services.web_risk_service.WebRiskServiceClient.html#google_cloud_webrisk_v1_services_web_risk_service_WebRiskServiceClient_search_hashes(request)\n\n # Get all the hashes that match the prefix. Cache the returned hashes until the time\n # specified in threat_hash.expire_time\n # For more information on response type, see:\n # https://cloud.google.com/web-risk/docs/reference/rpc/google.cloud.webrisk.v1#threathash\n hash_list = []\n for threat_hash in response.threats:\n hash_list.append(threat_hash.hash)\n return hash_list\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=webrisk)."]]