Buscar un hash
Organízate con las colecciones
Guarda y clasifica el contenido según tus preferencias.
Crear un cliente de Webrisk y buscar un hash
Investigar más
Para obtener documentación detallada que incluya este código de muestra, consulta lo siguiente:
Código de ejemplo
A menos que se indique lo contrario, el contenido de esta página está sujeto a la licencia Reconocimiento 4.0 de Creative Commons y las muestras de código están sujetas a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio web de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Es fácil de entender","easyToUnderstand","thumb-up"],["Me ofreció una solución al problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Es difícil de entender","hardToUnderstand","thumb-down"],["La información o el código de muestra no son correctos","incorrectInformationOrSampleCode","thumb-down"],["Me faltan las muestras o la información que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","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)."]]