转换参考

本主题介绍 Sensitive Data Protection 中提供的去标识化方法(或称转换)。

去标识化方法的类型

应根据要去标识化的数据种类以及对数据进行去标识化的目的来选择使用的去标识化转换。Sensitive Data Protection 支持的去标识化方法分为以下几大类别:

  • 隐去:删除检测到的全部或部分敏感值。
  • 替换:使用指定的代理值替换检测到的敏感值。
  • 遮罩:使用指定的代理字符(例如井号 (#) 或星号 (*))替换敏感值中的若干字符。
  • 基于加密的令牌化:使用加密密钥加密原始敏感数据值。Sensitive Data Protection 支持多种类型的令牌化,包括可以逆转或“重标识”的转换。
  • 分桶:使用某个范围或系列值替换敏感值来对其进行“泛化”处理。(例如,使用年龄范围来替换特定年龄值,或者使用“热”、“温”和“冷”替换相应范围内的温度值。)
  • 日期偏移:将敏感日期值移动一段随机时间。
  • 时间提取:提取或保留日期和时间值的指定部分。

本主题的其余部分将介绍各种不同类型的去标识化转换,并提供其使用示例。

转换方法

下表列出了 Sensitive Data Protection 提供的用于对敏感数据进行去标识化的转换方法:

转换 对象 说明 可以逆转1 参照完整性2 输入类型
遮盖 RedactConfig 通过删除值来进行遮盖。 不限
替换 ReplaceValueConfig 使用给定值替换每个输入值。 不限
替换为字典 ReplaceDictionaryConfig 将输入值替换为从字词列表中随机选择的值。 不限
使用 infoType 替换 ReplaceWithInfoTypeConfig 将输入值替换为其 infoType 的名称。 不限
使用字符遮盖 CharacterMaskConfig 通过将给定数量的字符替换为所指定的固定字符,全部或部分遮盖字符串。 不限
通过将输入值替换为加密哈希实现假名化 CryptoHashConfig 将输入值替换为由给定数据加密密钥生成的 32 字节十六进制字符串。请参阅假名化概念文档了解详情。 字符串或整数
通过替换为加密保留格式令牌实现假名化 CryptoReplaceFfxFpeConfig 使用 FFX 运算模式的保留格式加密 (FPE) 将输入值替换为长度相同的令牌或代理值。这样就能在具有长度格式验证的系统中使用输出。这对于必须保留字符串长度的旧系统很有用。重要提示:对于长度不等或长度超过 32 个字节的输入,请使用 CryptoDeterministicConfig。 为确保安全, 美国国家标准与技术研究院建议遵循以下限制:
  • radix^max_size <= 2^128.
  • radix^min_len >= 100
具有有限字符数或长度统一的字符串或整数。字母表必须至少包含 2 个字符,且不得超过 95 个字符。
通过替换为加密令牌实现假名化 CryptoDeterministicConfig 使用合成初始化矢量模式下的 AES (AES-SIV) 将输入值替换为长度相同的令牌或代理值。与保留格式令牌化不同,此转换方法对受支持的字符串字符集没有任何限制,为相同输入值的每个实例生成相同令牌,并在给定原始加密密钥的情况下使用代理来启用重标识。 不限
基于固定大小范围的分桶值 FixedSizeBucketingConfig 将输入值替换为输入值所在的分桶(或范围)的值。 不限
基于自定义大小范围的分桶值 BucketingConfig 根据用户可配置的范围和替换值将输入值替换为分桶值。 不限
日期偏移 DateShiftConfig 按随机天数偏移日期,可使同一上下文保持一致。
保留顺序和持续时间
日期/时间
提取时间数据 TimePartConfig 提取或保留 DateTimestampTimeOfDay 值的一部分。 日期/时间

脚注

1 可以对可逆转换进行逆转,以使用 content.reidentify 方法重新标识敏感数据。
2 参照完整性允许在对数据去标识化的同时保持记录之间的关系。例如,给定相同的加密密钥和上下文,数据在每次转换时都将被替换为相同的去标识化形式,从而保留记录之间的联系。

隐去

如果只想从输入内容中移除敏感数据,Sensitive Data Protection 支持隐去转换(DLP API 中的 RedactConfig)。

例如,假设您要对所有 EMAIL_ADDRESS infoType 执行简单的隐去处理,且下列字符串已发送到 Sensitive Data Protection:

My name is Alicia Abernathy, and my email address is aabernathy@example.com.

返回的字符串如下:

My name is Alicia Abernathy, and my email address is .

多种语言的以下 JSON 示例和代码演示了如何构建 API 请求以及 DLP API 会返回哪些内容。

C#

如需了解如何安装和使用 Sensitive Data Protection 客户端库,请参阅 Sensitive Data Protection 客户端库

如需向 Sensitive Data Protection 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证


using System;
using System.Collections.Generic;
using Google.Api.Gax.ResourceNames;
using Google.Cloud.Dlp.V2;

public class DeidentifyDataUsingRedactWithMatchedInputValues
{
    public static DeidentifyContentResponse Deidentify(
        string projectId,
        string text,
        IEnumerable<InfoType> infoTypes = null)
    {
        // Instantiate the client.
        var dlp = DlpServiceClient.Create();

        // Construct inspect config.
        var inspectConfig = new InspectConfig
        {
            InfoTypes = { infoTypes ?? new InfoType[] { new InfoType { Name = "EMAIL_ADDRESS" } } },
        };

        // Construct redact config.
        var redactConfig = new RedactConfig();

        // Construct deidentify config using redact config.
        var deidentifyConfig = new DeidentifyConfig
        {
            InfoTypeTransformations = new InfoTypeTransformations
            {
                Transformations =
                {
                    new InfoTypeTransformations.Types.InfoTypeTransformation
                    {
                        PrimitiveTransformation = new PrimitiveTransformation
                        {
                            RedactConfig = redactConfig
                        }
                    }
                }
            }
        };

        // Construct a request.
        var request = new DeidentifyContentRequest
        {
            ParentAsLocationName = new LocationName(projectId, "global"),
            DeidentifyConfig = deidentifyConfig,
            InspectConfig = inspectConfig,
            Item = new ContentItem { Value = text }
        };

        // Call the API.
        var response = dlp.DeidentifyContent(request);

        // Check the deidentified content.
        Console.WriteLine($"Deidentified content: {response.Item.Value}");
        return response;
    }
}

Go

如需了解如何安装和使用 Sensitive Data Protection 客户端库,请参阅 Sensitive Data Protection 客户端库

如需向 Sensitive Data Protection 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证

import (
	"context"
	"fmt"
	"io"

	dlp "cloud.google.com/go/dlp/apiv2"
	"cloud.google.com/go/dlp/apiv2/dlppb"
)

// deidentifyWithRedact de-identify the data by redacting with matched input values
func deidentifyWithRedact(w io.Writer, projectID, inputStr string, infoTypeNames []string) error {
	// projectID := "my-project-id"
	// inputStr := "My name is Alicia Abernathy, and my email address is aabernathy@example.com."
	// infoTypeNames := []string{"EMAIL_ADDRESS"}

	ctx := context.Background()

	// Initialize a client once and reuse it to send multiple requests. Clients
	// are safe to use across goroutines. When the client is no longer needed,
	// call the Close method to cleanup its resources.
	client, err := dlp.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("dlp.NewClient: %w", err)
	}

	// Closing the client safely cleans up background resources.
	defer client.Close()

	// Specify the content to be inspected.
	contentItem := &dlppb.ContentItem{
		DataItem: &dlppb.ContentItem_Value{
			Value: inputStr,
		},
	}

	// Specify the type of info the inspection will look for.
	// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
	var infoTypes []*dlppb.InfoType
	for _, it := range infoTypeNames {
		infoTypes = append(infoTypes, &dlppb.InfoType{Name: it})
	}
	inspectConfig := &dlppb.InspectConfig{
		InfoTypes: infoTypes,
	}

	// Define type of de-identification.
	primitiveTransformation := &dlppb.PrimitiveTransformation{
		Transformation: &dlppb.PrimitiveTransformation_RedactConfig{
			RedactConfig: &dlppb.RedactConfig{},
		},