정의
버전 8.3에 추가 되었습니다.
구문
$hash 의 구문은 다음과 같습니다:
{ $hash: { input: <expression>, algorithm: <string> } }
행동
input 값 | 결과 | 참고 사항 |
|---|---|---|
UTF-8 문자열 | input must be a valid UTF-8 string. | |
|
|
input 가 null 로 해석되거나 정의되지 않았거나 누락된 필드 참조하는 경우 $hash 는 null을 반환합니다.
input 이 UTF-8 string 또는 BinData 이외의 유형으로 해석되는 경우 $hash 는 오류를 반환합니다.
algorithm 이(가) 허용되는 값 중 하나가 아닌 경우 $hash 은(는) 오류를 반환합니다.
중요
MD5 는 암호학적으로 안전한 알고리즘 아니며 보안에 민감한 애플리케이션에 적합하지 않습니다. 민감한 데이터를 해싱하는 경우 "sha256" 를 대신 사용합니다.
MD5 도 FIPS 모드 에서 비활성화됩니다. FIPS 모드 활성화된 상태에서 배포서버 실행되는 경우 "sha256" 또는 "xxh64" 를 대신 사용합니다.
예시
다음 예제에서는 이러한 문서에 files 라는 이름의 컬렉션 사용합니다.
db.files.insertMany( [ { _id: 1, filename: "report.pdf" }, { _id: 2, filename: "archive.zip" } ] )
필드 값 해시
다음 예시 각 document 에 대해 filename 필드 의 SHA-256 해시를 계산합니다.
db.files.aggregate( [ { $project: { filename: 1, hash: { $hash: { input: "$filename", algorithm: "sha256" } } } } ] )
[ { _id: 1, filename: 'report.pdf', hash: Binary.createFromBase64('ZGbkUKFrd7hlxYKda2xW2fiSlWR1ZC2+uczENA/gGxU=', 0) }, { _id: 2, filename: 'archive.zip', hash: Binary.createFromBase64('jsO6p+AcJW/43CrXux9Tpmq+hHLmCKPmqJjcARAsmVA=', 0) } ]
리터럴 string 해시
다음 예시 리터럴 string 값을 해시합니다.
db.aggregate( [ { $documents: [ { val: "hello" } ] }, { $project: { _id: 0, hash: { $hash: { input: "$val", algorithm: "xxh64" } } } } ] )
[ { hash: Binary.createFromBase64('JseCfYifbaM=', 0) } ]
해시 빈 데이터
다음 예시 binary 데이터 필드 의 SHA-256 해시를 계산합니다.
db.binaries.insertMany( [ { _id: 1, data: new BinData(0, "aGVsbG8=") } ] )
db.binaries.aggregate( [ { $project: { hash: { $hash: { input: "$data", algorithm: "sha256" } } } } ] )
[ { _id: 1, hash: Binary.createFromBase64('LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=', 0) } ]
Null 또는 누락된 input
input 가 null 이거나 누락된 필드 참조하는 경우 $hash 는 null을 반환합니다.
db.aggregate( [ { $documents: [ { val: null }, {} ] }, { $project: { hash: { $hash: { input: "$val", algorithm: "sha256" } } } } ] )
[ { hash: null }, { hash: null } ]