

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 從 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*

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

此模式說明如何在從 SQL Server 遷移至 Amazon RDS for PostgreSQL 或 Amazon Aurora PostgreSQL 相容時，為電子郵件地址實作安全雜湊演算法 1 (SHA1) 雜湊。電子郵件地址是*個人身分識別資訊* (PII) 的範例。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 資料庫
+ Target 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 或更新版本

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

**來源技術堆疊 **
+ SQL Server
+ .NET Framework

**目標技術堆疊 **
+ PostgreSQL
+ `pgcrypto` 延伸模組

**自動化和擴展**
+ 請考慮實作雜湊函數作為預存程序，以便於維護。
+ 對於大型資料集，請評估效能，並考慮批次處理或索引策略。

## 工具
<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)可協助您在 中設定、操作和擴展 PostgreSQL 關聯式資料庫 AWS 雲端。
+ [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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 檢閱 SQL Server 程式碼。 | 若要檢閱產生 SHA1 雜湊的 SQL Server 程式碼，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 資料工程師、DBA、應用程式開發人員 | 
| 記錄雜湊演算法和資料轉換。 | 若要記錄確切的雜湊演算法和資料轉換，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 應用程式開發人員、資料工程師、DBA | 

### 建立 PostgreSQL 雜湊函數
<a name="create-postgresql-hashing-function"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立`pgcrypto`擴充功能。 | 若要建立`pgcrypto`擴充功能，請使用 `pgAdmin/psql`執行下列命令：<pre>CREATE EXTENSION pgcrypto;</pre> | DBA，資料工程師 | 
| 實作 PostgreSQL 函數。 | 實作下列 PostgreSQL 函數來複寫 SQL Server 雜湊邏輯。在高階，此函數使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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> | 資料工程師、DBA、應用程式開發人員 | 
| 測試函數。 | 若要測試函數，請使用 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> | 應用程式開發人員、DBA、資料工程師 | 

### 實作自動雜湊的觸發
<a name="implement-triggers-for-automatic-hashing"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在相關資料表上建立觸發條件。 | 若要在相關資料表上建立觸發，以在插入或更新時自動產生雜湊值，請執行下列命令：<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> | 應用程式開發人員、資料工程師、DBA | 

### 遷移現有資料
<a name="migrate-existing-data"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 開發遷移指令碼或使用 AWS DMS。 | 開發遷移指令碼或使用 AWS DMS 來填入現有資料的雜湊值 （包括存放在`BIGINT`來源系統中的雜湊值）。完成下列任務：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 資料工程師、應用程式開發人員、DBA | 
| 使用新的 PostgreSQL 雜湊函數。 | 若要使用新的 PostgreSQL 雜湊函數以確保一致性，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 應用程式開發人員、DBA、DevOps 工程師 | 

### 更新應用程式查詢
<a name="update-application-queries"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 識別應用程式查詢。 | 若要識別使用雜湊值的應用程式查詢，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 應用程式開發人員、DBA、資料工程師 | 
| 修改查詢。 | 如有必要，請修改查詢以使用新的 PostgreSQL 雜湊函數。請執行下列操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 應用程式開發人員、DBA、資料工程師 | 

### 測試和驗證
<a name="test-and-validate"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 執行測試。 | 若要使用生產資料子集執行徹底測試，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 應用程式開發人員、資料工程師、DBA | 
| 驗證雜湊值是否相符。 | 若要驗證 SQL Server 和 PostgreSQL 之間的雜湊值是否相符，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 應用程式開發人員、資料工程師、DBA | 
| 驗證應用程式功能。 | 若要使用遷移的資料和新的雜湊實作來驗證應用程式功能，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/implement-sha1-hashing-for-pii-data-when-migrating-from-sql-server-to-postgresql.html) | 應用程式開發人員、DBA、資料工程師 | 

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


| 問題 | 解決方案 | 
| --- | --- | 
| 雜湊值不相符。 | 驗證來源和目標之間的字元編碼和定序。如需詳細資訊，請參閱在 [Amazon Aurora 和 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 文件）