해시 검색
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
Webrisk 클라이언트 만들기 및 해시 검색
더 살펴보기
이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.
코드 샘플
Java
Web Risk에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다.
자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.
Python
Web Risk에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다.
자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 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)."]]