创建 Webrisk 客户端并搜索哈希值
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
Java
如需向 Web Risk 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
import com.google.cloud.webrisk.v1.WebRiskServiceClient;
import com.google.protobuf.ByteString;
import com.google.webrisk.v1.SearchHashesRequest;
import com.google.webrisk.v1.SearchHashesResponse;
import com.google.webrisk.v1.SearchHashesResponse.ThreatHash;
import com.google.webrisk.v1.ThreatType;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
public class SearchHashes {
public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
// TODO(developer): Replace these variables before running the sample.
// A hash prefix, consisting of the most significant 4-32 bytes of a SHA256 hash.
// For JSON requests, this field is base64-encoded. Note that if this parameter is provided
// by a URI, it must be encoded using the web safe base64 variant (RFC 4648).
String uri = "http://example.com";
String encodedUri = Base64.getUrlEncoder().encodeToString(uri.getBytes(StandardCharsets.UTF_8));
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] encodedHashPrefix = digest.digest(encodedUri.getBytes(StandardCharsets.UTF_8));
// The ThreatLists to search in. Multiple ThreatLists may be specified.
// For the list on threat types, see: https://cloud.google.com/web-risk/docs/reference/rpc/google.cloud.webrisk.v1#threattype
List<ThreatType> threatTypes = Arrays.asList(ThreatType.MALWARE, ThreatType.SOCIAL_ENGINEERING);
searchHash(ByteString.copyFrom(encodedHashPrefix), threatTypes);
}
// Gets the full hashes that match the requested hash prefix.
// This is used after a hash prefix is looked up in a threatList and there is a match.
// The client side threatList only holds partial hashes so the client must query this method
// to determine if there is a full hash match of a threat.
public static void searchHash(ByteString encodedHashPrefix, List<ThreatType> threatTypes)
throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the `webRiskServiceClient.close()` method on the client to safely
// clean up any remaining background resources.
try (WebRiskServiceClient webRiskServiceClient = WebRiskServiceClient.create()) {
// Set the hashPrefix and the threat types to search in.
SearchHashesResponse response = webRiskServiceClient.searchHashes(
SearchHashesRequest.newBuilder()
.setHashPrefix(encodedHashPrefix)
.addAllThreatTypes(threatTypes)
.build());
// Get all the hashes that match the prefix. Cache the returned hashes until the time
// specified in threatHash.getExpireTime()
// For more information on response type, see: https://cloud.google.com/web-risk/docs/reference/rpc/google.cloud.webrisk.v1#threathash
for (ThreatHash threatHash : response.getThreatsList()) {
System.out.println(threatHash.getHash());
}
System.out.println("Completed searching threat hashes.");
}
}
}
Python
如需向 Web Risk 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
from google.cloud import webrisk_v1
def search_hashes(hash_prefix: bytes, threat_type: webrisk_v1.ThreatType) -> list:
"""Gets the full hashes that match the requested hash prefix.
This is used after a hash prefix is looked up in a threatList and there is a match.
The client side threatList only holds partial hashes so the client must query this method
to determine if there is a full hash match of a threat.
Args:
hash_prefix: A hash prefix, consisting of the most significant 4-32 bytes of a SHA256 hash.
For JSON requests, this field is base64-encoded. Note that if this parameter is provided
by a URI, it must be encoded using the web safe base64 variant (RFC 4648).
Example:
uri = "http://example.com"
sha256 = sha256()
sha256.update(base64.urlsafe_b64encode(bytes(uri, "utf-8")))
hex_string = sha256.digest()
threat_type: The ThreatLists to search in. Multiple ThreatLists may be specified.
For the list on threat types, see:
https://cloud.google.com/web-risk/docs/reference/rpc/google.cloud.webrisk.v1#threattype
threat_type = [webrisk_v1.ThreatType.MALWARE, webrisk_v1.ThreatType.SOCIAL_ENGINEERING]
Returns:
A hash list that contain all hashes that matches the given hash prefix.
"""
webrisk_client = webrisk_v1.WebRiskServiceClient()
# Set the hashPrefix and the threat types to search in.
request = webrisk_v1.SearchHashesRequest()
request.hash_prefix = hash_prefix
request.threat_types = [threat_type]
response = webrisk_client.search_hashes(request)
# Get all the hashes that match the prefix. Cache the returned hashes until the time
# specified in threat_hash.expire_time
# For more information on response type, see:
# https://cloud.google.com/web-risk/docs/reference/rpc/google.cloud.webrisk.v1#threathash
hash_list = []
for threat_hash in response.threats:
hash_list.append(threat_hash.hash)
return hash_list
后续步骤
如需搜索并过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。