Migrate policy cloud SHA256 callers to crypto/hash.h

IN REVIEW2026components/policycrypto
2026. 9. 5.Ji-Hyeon212 프로필 이미지Ji-Hyeon212

//crypto 해시 API 마이그레이션 작업의 일부로,
components/policy/core/common/cloud 디렉터리에서 사용하던 구 API
crypto/sha2.hcrypto::SHA256HashString() 호출부를 신규 API인
crypto/hash.hcrypto::hash::Sha256() 기반으로 교체했습니다.

문제 설명

Chromium에서는 //crypto API를 기능별 하위 네임스페이스로 정리하는
마이그레이션이 진행되고 있습니다. 이 과정에서 crypto/sha2.h는 제거될
예정이므로, 기존 SHA-256 호출부를 crypto/hash.h가 제공하는 새 API로
옮겨야 했습니다.

이번 작업 범위는 components/policy/core/common/cloud 하위 파일입니다.
해당 코드들은 component cloud policy 및 external policy data를 캐시에서
로드하거나 검증할 때 SHA-256 해시를 사용하고 있었습니다.

// 구 API - crypto/sha2.h
namespace crypto {
  CRYPTO_EXPORT std::string SHA256HashString(std::string_view str);
}

// 신 API - crypto/hash.h
namespace crypto::hash {
  CRYPTO_EXPORT std::array<uint8_t, kSha256Size> Sha256(
      base::span<const uint8_t> data);
}

기존 SHA256HashString()은 raw SHA-256 결과를 std::string으로 반환하지만,
신규 Sha256()std::array<uint8_t, 32> 형태를 반환합니다. 따라서 기존
코드가 기대하던 raw hash string 동작을 유지하기 위한 변환이 필요했습니다.

해결 내용

1. 공통 API 치환

components/policy/core/common/cloud 하위 9개 파일에서 deprecated include와
SHA-256 호출부를 교체했습니다.

- #include "crypto/sha2.h"
+ #include "crypto/hash.h"

- crypto::SHA256HashString(x)
+ std::string(base::as_string_view(crypto::hash::Sha256(x)))

base::as_string_view() 사용을 위해 필요한 파일에는 아래 include를
추가했습니다.

+ #include "base/strings/string_view_util.h"

2. Raw hash string 동작 유지

기존 crypto::SHA256HashString()은 hex 문자열이 아니라 raw SHA-256 32바이트를
담은 std::string을 반환했습니다. 새 API의 반환값은 std::array<uint8_t, 32>
이므로, 기존 비교 및 저장 로직과 맞추기 위해 base::as_string_view()
std::string 변환을 사용했습니다.

std::string(base::as_string_view(crypto::hash::Sha256(data)))

3. 문자열 상수 해시 입력 보정

테스트 코드에서 문자열 상수를 직접 Sha256()에 전달할 경우, 기존
SHA256HashString()과 해시 대상 바이트 범위가 달라질 수 있었습니다.
ComponentCloudPolicyServiceTest.LoadInvalidPolicyFromCache 테스트에서 이 차이로
캐시 데이터와 secure_hash가 일치하지 않아 policy가 버려지는 문제가
발생했습니다.

이를 해결하기 위해 kInvalidTestPolicystd::string_view로 감싸 기존처럼
문자열 내용만 해시하도록 수정했습니다.

- crypto::hash::Sha256(kInvalidTestPolicy)
+ crypto::hash::Sha256(std::string_view(kInvalidTestPolicy))

4. 파일별 주요 변경

cloud_external_data_manager.cc에서는 MetadataKey::ToString()에서 policy hash와
field name hash를 생성한 뒤 기존처럼 이어붙여 metadata key를 만들도록
유지했습니다.

- return base::StrCat(
-     {crypto::SHA256HashString(policy), crypto::SHA256HashString(field_name)});
+ return base::StrCat({base::as_string_view(crypto::hash::Sha256(policy)),
+                      base::as_string_view(crypto::hash::Sha256(field_name))});

cloud_external_data_store.ccexternal_policy_data_updater.cc에서는 external
policy data 검증 로직을 새 API 기반으로 변경했습니다.

- crypto::SHA256HashString(*data) == hash
+ std::string(base::as_string_view(crypto::hash::Sha256(*data))) == hash
- crypto::SHA256HashString(*data) != request_.hash
+ std::string(base::as_string_view(crypto::hash::Sha256(*data))) !=
+     request_.hash

component_cloud_policy_store.cc에서는 policy data의 secure_hash 비교 로직을
새 API 기반으로 변경했습니다.

- if (crypto::SHA256HashString(data) != secure_hash) {
+ if (std::string(base::as_string_view(crypto::hash::Sha256(data))) !=
+     secure_hash) {

component_cloud_policy_store_unittest.cc에서는 테스트용 policy hash helper를
새 API 기반으로 변경했습니다.

- return crypto::SHA256HashString(kTestPolicy);
+ return std::string(base::as_string_view(crypto::hash::Sha256(kTestPolicy)));

component_cloud_policy_service_unittest.cc에서는 테스트용 secure_hash 생성
로직을 새 API 기반으로 변경하고, invalid policy cache 테스트에서는 문자열 상수
입력 범위를 기존 동작과 맞추었습니다.

- builder_.payload().set_secure_hash(crypto::SHA256HashString(kInvalidTestPolicy));
+ builder_.payload().set_secure_hash(std::string(base::as_string_view(
+     crypto::hash::Sha256(std::string_view(kInvalidTestPolicy)))));

cloud_external_data_store_unittest.cc,
component_cloud_policy_updater_unittest.cc,
external_policy_data_updater_unittest.cc에서는 테스트 데이터의 expected hash 생성
로직을 crypto::hash::Sha256() 기반으로 변경했습니다.

테스트 방법

아래 명령으로 포맷, 빌드, 관련 단위 테스트 및 presubmit을 확인했습니다.

git cl format
autoninja -C out/Default -j 4 components_unittests
out/Default/components_unittests \
  --gtest_filter='ComponentCloudPolicyServiceTest.LoadInvalidPolicyFromCache'
git cl presubmit

추가로 기존 API가 작업 범위에 남아 있지 않은지 확인했습니다.

grep -R "crypto/sha2.h" -n components/policy/core/common/cloud
grep -R "crypto::SHA256" -n components/policy/core/common/cloud

ComponentCloudPolicyServiceTest.LoadInvalidPolicyFromCache 테스트는 최초 변경 후
secure_hash와 캐시 데이터의 hash mismatch로 실패했으나, 문자열 상수 입력을
std::string_view로 보정한 뒤 통과했습니다.

배운 점

  • autoninja -C out/Default components_unittests는 테스트 바이너리를 빌드하는
    단계이고, 실제 테스트 실행은 out/Default/components_unittests --gtest_filter=...로 별도로 수행해야 한다는 점을 확인했습니다.
  • crypto::SHA256HashString()crypto::hash::Sha256()는 반환 타입이 다르므로
    단순 함수명 치환만으로는 기존 raw hash string 동작을 보존할 수 없습니다.
  • 문자열 상수를 새 Sha256() API에 넘길 때는 기존 API와 동일한 입력 범위를
    유지해야 합니다. 필요하면 std::string_view를 사용해 null terminator가 해시
    입력에 포함되지 않도록 해야 합니다.
  • CQ Dry Run은 로컬 빌드보다 넓은 환경에서 테스트를 실행하므로, 로컬에서
    빌드만 성공한 변경도 실제 테스트 실행 단계에서 동작 차이가 드러날 수
    있습니다.

참고 자료