

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 从 SQL Server 迁移到 PostgreSQL 时，对 PII 数据实施 SHA1 哈希处理
<a name="implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql"></a>

*Rajkumar Raghuwanshi 和 Jagadish Kantubugata，Amazon Web Services*

## Summary
<a name="implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql-summary"></a>

此模式描述了在从 SQL Server 迁移到适用于 PostgreSQL 的亚马逊 RDS 或兼容 Amazon Aurora PostgreSQL 时，如何对电子邮件地址实施安全哈希算法 1 (SHA1) 哈希。电子邮件地址是*个人身份信息*（PII）的一个示例。直接查看其他相关数据或与之配对时可用于合理推断个人身份的信息。

此模式介绍了在不同的数据库归类和字符编码中保持一致的哈希值所面临的挑战，并提供了使用 PostgreSQL 函数和触发器的解决方案。尽管这种模式侧重于 SHA1 哈希，但它可以适应PostgreSQL模块支持的其他哈希算法。`pgcrypto`请务必考虑哈希算法策略的安全影响，如果要处理敏感数据，请咨询安全专家。

## 先决条件和限制
<a name="implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql-prereqs"></a>

**先决条件**
+ 活跃的 AWS 账户
+ 检查源 SQL Server 数据库。
+ 目标 PostgreSQL 数据库（Amazon RDS for PostgreSQL 或 Aurora PostgreSQL 兼容版）
+ PL/pgSQL 编码专业知识

**限制**
+ 此模式需要根据使用案例更改数据库级别的排序规则。
+ 尚未评估对大型数据集的性能影响。
+ 有些 AWS 服务 并非全部可用 AWS 区域。有关区域可用性，请参阅[按区域划分的AWS 服务](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。有关特定端点，请参阅[服务端点和配额](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)，然后选择相应服务的链接。

**产品版本**
+ Microsoft SQL Server 2012 或更高版本

## 架构
<a name="implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql-architecture"></a>

**源技术堆栈**
+ SQL Server
+ NET Framework。

**目标技术堆栈**
+ PostgreSQL
+ `pgcrypto`extension

**自动化和扩展**
+ 为了便于维护，可以考虑将哈希函数作为存储过程来实现。
+ 对于大型数据集，则可评估性能并考虑批处理或索引策略。

## 工具
<a name="implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql-tools"></a>

**AWS 服务**
+ [Amazon Aurora PostgreSQL 兼容版](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.AuroraPostgreSQL.html)是一个完全托管式、与 ACID 兼容的关系数据库引擎，可帮助您建立、运行和扩缩 PostgreSQL 部署。
+ [AWS Database Migration Service (AWS DMS)](https://docs.aws.amazon.com/dms/latest/userguide/Welcome.html) 可帮助您将数据存储迁移到云和本地设置的组合中， AWS 云 或者迁移到云端和本地设置的组合之间。
+ [Amazon Relational Database Service Amazon RDS for PostgreSQL ](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html)可帮助您在 AWS 云中设置、操作和扩展 PostgreSQL 关系数据库。
+ [AWS Schema Conversion Tool (AWS SCT)](https://docs.aws.amazon.com/SchemaConversionTool/latest/userguide/CHAP_Welcome.html) 通过自动将源数据库架构和大部分自定义代码转换为与目标数据库兼容的格式来支持异构数据库迁移。

**其他工具**
+ [pgAdmin](https://www.pgadmin.org/) 是一种适用于 PostgreSQL 的开源管理工具。它提供了一个图形界面，可帮助您创建、维护和使用数据库对象。
+ [SQL Server Management Studio（SSMS）](https://learn.microsoft.com/en-us/ssms/sql-server-management-studio-ssms)是用于管理 任何 SQL 基础设施的集成环境。

## 最佳实践
<a name="implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql-best-practices"></a>
+ 使用适当的归类设置来处理目标数据库端的特殊字符。
+ 使用各种电子邮件地址（包括带有非 ASCII 字符的地址）进行彻底全面的测试。
+ 保持应用程序层和数据库层之间大写和小写处理的一致性。
+ 使用哈希值对查询的性能进行基准测试。

## 操作说明
<a name="implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql-epics"></a>

### 分析源哈希算法实现
<a name="analyze-source-hashing-implementation"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 查看 SQL 服务器代码。 | 要查看生成 SHA1 哈希值的 SQL Server 代码，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 数据工程师、数据库管理员、应用程序开发人员 | 
| 要记录确切的哈希算法和数据转换，请执行以下操作： | 要记录确切的哈希算法和数据转换，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 应用程序开发人员、数据工程师、数据库管理员 | 

### 创建 PostgreSQL 哈希函数
<a name="create-postgresql-hashing-function"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建 `pgcrypto` 扩展。 | 要创建 `pgcrypto` 扩展，请使用 `pgAdmin/psql` 运行以下命令：<pre>CREATE EXTENSION pgcrypto;</pre> | 数据库管理员、数据工程师 | 
| 实现 PostgreSQL 函数。 | 实现以下 PostgreSQL 函数来复制 SQL Server 的哈希逻辑。简而言之，此函数会使用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html)<pre>CREATE OR REPLACE FUNCTION utility.hex_to_bigint ( <br />     par_val character varying, <br />     par_upper character varying DEFAULT 'lower'::character varying) <br />RETURNS bigint <br />LANGUAGE 'plpgsql' <br />AS $BODY$ <br />DECLARE <br />    retnumber bigint; <br />    digest_bytes bytea;<br />BEGIN <br />    if lower(par_upper) = 'upper' <br />    then <br />        digest_bytes := digest(upper(par_val), 'sha1');<br />    else <br />        digest_bytes := digest((par_val), 'sha1');<br />    end if; <br />    retnumber := ('x' || encode(substring(digest_bytes, length(digest_bytes)-10+1), 'hex'))::bit(64)::bigint; <br />    RETURN retnumber; <br />END; <br />$BODY$;</pre> | 数据工程师、数据库管理员、应用程序开发人员 | 
| 测试函数。 | 要测试该函数，请使用来自 SQL Server 的示例数据来验证匹配的哈希值。运行如下命令：<pre>select 'alejandro_rosalez@example.com' as Email, utility.hex_to_bigint('alejandro_rosalez@example.com','upper') as HashValue;<br /><br />--OUTPUT<br />/*<br />email 	        hashvalue<br />"alejandro_rosalez@example.com"	451397011176045063<br />*/<br /></pre> | 应用程序开发人员、数据库管理员、数据工程师 | 

### 实现自动哈希触发器
<a name="implement-triggers-for-automatic-hashing"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 在相关表上创建触发器。 | 要在相关表上创建触发器以在插入或更新时自动生成哈希值，请运行以下命令：<pre>CREATE OR REPLACE FUNCTION update_email_hash() <br />RETURNS TRIGGER <br />AS $$ <br />BEGIN <br />    NEW.email_hash = utility.hex_to_bigint(NEW.email, 'upper'); <br />    RETURN NEW; <br />END; <br />$$ LANGUAGE plpgsql;</pre><pre>CREATE TRIGGER email_hash_trigger BEFORE INSERT OR UPDATE ON users FOR EACH ROW EXECUTE FUNCTION update_email_hash();</pre> | 应用程序开发人员、数据工程师、数据库管理员 | 

### 迁移现有数据
<a name="migrate-existing-data"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 开发迁移脚本或使用 AWS DMS。 | 开发迁移脚本或使用 AWS DMS 为现有数据（包括存储在源系统`BIGINT`中的哈希值）填充哈希值。完成以下任务：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 数据工程师、应用程序开发人员、数据库管理员 | 
| 使用新的 PostgreSQL 哈希函数。 | 要使用新的 PostgreSQL 哈希函数以确保一致性，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 应用程序开发人员、数据库管理员、 DevOps 工程师 | 

### 更新应用程序查询
<a name="update-application-queries"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 识别应用程序查询。 | 要识别使用哈希值的应用程序查询，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 应用程序开发人员、数据库管理员、数据工程师 | 
| 修改查询。 | 如有必要，可以修改查询以使用新的 PostgreSQL 哈希函数。执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 应用程序开发人员、数据库管理员、数据工程师 | 

### 测试和评估
<a name="test-and-validate"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 执行测试。 | 要对生产数据的子集执行全面的测试，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 应用程序开发人员、数据工程师、数据库管理员 | 
| 验证哈希值是否匹配。 | 要验证 SQL Server 和 PostgreSQL 之间的哈希值是否匹配，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 应用程序开发人员、数据工程师、数据库管理员 | 
| 验证应用程序功能。 | 要使用迁移的数据和新的哈希实现来验证应用程序功能，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 应用程序开发人员、数据库管理员、数据工程师 | 

## 问题排查
<a name="implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
| 哈希值不匹配。 | 验证源和目标之间的字符编码和排序规则。有关更多信息，请参阅在 Amazon A [urora 和 Amazon RDS 上管理 PostgreSQL 中的排序规则更改（博](https://aws.amazon.com/blogs/database/manage-collation-changes-in-postgresql-on-amazon-aurora-and-amazon-rds/)客）。AWS  | 

## 相关资源
<a name="implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql-resources"></a>

**AWS 博客**
+ [在 Amazon Aurora 和 Amazon RDS 上管理 PostgreSQL 中的排序规则更改](https://aws.amazon.com/blogs/database/manage-collation-changes-in-postgresql-on-amazon-aurora-and-amazon-rds/)
+ [采用最佳实践并结合来自实际应用中的经验教训，将 SQL Server 迁移到 Amazon Aurora PostgreSQL](https://aws.amazon.com/blogs/database/migrate-sql-server-to-amazon-aurora-postgresql-using-best-practices-and-lessons-learned-from-the-field/)

**其他资源**
+ [PostgreSQL pgcrypto 模块](https://www.postgresql.org/docs/current/pgcrypto.html)（PostgreSQL 文档）
+ [PostgreSQL 触发函数](https://www.postgresql.org/docs/current/plpgsql-trigger.html)（PostgreSQL 文档）
+ [SQL Server HASHBYTES 函数](https://docs.microsoft.com/en-us/sql/t-sql/functions/hashbytes-transact-sql)（Microsoftś文档）