ClientEncryption.encrypt(keyId, value, algorithm or encOptions)ClientEncryption.encrypt()encrypts thevalueusing the specifiedkeyIdand the algorithm specified byalgorithmorencOptions.encrypt()supports explicit (manual) encryption of field values.반환합니다: 하위 유형이 6 인
binary data객체 입니다.
호환성
이 명령은 다음 환경에서 호스팅되는 배포에서 사용할 수 있습니다.
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: MongoDB의 구독 기반 자체 관리 버전
MongoDB Community: MongoDB의 소스 사용 가능 무료 자체 관리 버전
구문
clientEncryption = db.getMongo().getClientEncryption() clientEncryption.encrypt( keyId, value, algorithm or encOptions, )
Parameter | 유형 | 설명 |
|---|---|---|
|
|
The UUID is a BSON |
| 암호화할 값입니다. | |
| 문자열 또는 문서 |
|
행동
The mongosh client-side field level and queryable encryption methods require a database connection configured for client-side encryption. If the current database connection was not initiated with client-side field level encryption enabled, either:
mongosh의Mongo()생성자를 사용하여 필요한 클라이언트 사이드 필드 수준 암호화 옵션과 연결을 설정합니다.Mongo()메서드는 고객 마스터 키(CMK) 관리를 위해 다음과 같은 키 관리 서비스(KMS) 공급자를 지원합니다.
or
- Use the
mongoshcommand line options to establish a connection with the required options. The command line options only support the Amazon Web Services KMS provider for CMK management.
지원되지 않는 BSON type
You cannot use encrypt() to encrypt values with the following BSON types:
minKeymaxKeynullundefined
If encrypting a field using AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic, encrypt() does not support the following BSON types:
doubledecimal128boolobjectarray
예시
클라이언트 사이드 필드 레벨 암호화
다음 예에서는 클라이언트 사이드 필드 수준 암호화 구성을 위해 로컬로 managed KMS를 사용합니다.
암호화된 연결 만들기
mongosh 시작
실행:
mongosh --nodb --nodb데이터베이스 에 연결하지 않음을 의미합니다.키 string생성
기본 64 96바이트 string 을 생성합니다.
const TEST_LOCAL_KEY = require("crypto").randomBytes(96).toString("base64") 암호화 옵션 객체 만들기
클라이언트 사이드 필드 수준 암호화 옵션 객체 를 만들려면 이전 단계의
TEST_LOCAL_KEYstring 을 사용합니다.var autoEncryptionOpts = { "keyVaultNamespace" : "encryption.__dataKeys", "kmsProviders" : { "local" : { "key" : BinData(0, TEST_LOCAL_KEY) } } } 암호화된 클라이언트 객체 만들기
암호화됨 클라이언트 객체 를 만들려면
Mongo()생성자를 사용합니다.mongodb://myMongo.example.netURI를 대상 클러스터 의 연결 string URI 로 바꿉니다. 예를 예시 다음과 같습니다.encryptedClient = Mongo( "mongodb://myMongo.example.net:27017/?replSetName=myMongo", autoEncryptionOpts )
데이터 암호화
Retrieve the ClientEncryption object and use the ClientEncryption.encrypt() method to encrypt a value using a specific data encryption key UUID and encryption algorithm:
clientEncryption = encryptedClient.getClientEncryption(); clientEncryption.encrypt( UUID("64e2d87d-f168-493c-bbdf-a394535a2cb9"), "123-45-6789", "AEAD_AES_256_CBC_HMAC_SHA_512-Random" )
algorithm 필드가 있는 문서를 사용하여 알고리즘을 지정할 수도 있습니다.
clientEncryption = encryptedClient.getClientEncryption(); clientEncryption.encrypt( UUID("64e2d87d-f168-493c-bbdf-a394535a2cb9"), "123-45-6789", { algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Random" } )
결과 예시
If successful, encrypt() returns the encrypted value:
BinData(6,"AmTi2H3xaEk8u9+jlFNaLLkC3Q/+kmwDbbWrq+h9nuv9W+u7A5a0UnpULBNZH+Q21fAztPpU09wpKPrju9dKfpN1Afpj1/ZhFcH6LYZOWSBBOAuUNjPLxMNSYOOuITuuYWo=")
클라이언트 사이드 필드 레벨 암호화가 활성화된 상태에서 MongoDB 연결을 시작하는 방법에 대한 전체 설명서는 Mongo() 를 참조하세요.
Queryable Encryption
다음 예에서는 Queryable Encryption 구성을 위해 로컬로 관리되는 KMS를 사용합니다.
암호화된 연결 만들기
mongosh 시작
mongosh클라이언트를 시작합니다.mongosh --nodb 키 생성
로컬로 관리되는 키에 대해 Queryable Encryption 을 구성하려면 줄 바꿈 없이 기본64인코딩된 96바이트 string 을 생성합니다.
const TEST_LOCAL_KEY = require("crypto").randomBytes(96).toString("base64") Queryable Encryption 옵션 생성
생성된 로컬 키 문자열을 사용하여 Queryable Encryption 옵션을 생성합니다.
var autoEncryptionOpts = { "keyVaultNamespace" : "encryption.__dataKeys", "kmsProviders" : { "local" : { "key" : BinData(0, TEST_LOCAL_KEY) } } } 암호화된 클라이언트 만들기
Queryable Encryption옵션이 구성된
Mongo()생성자를 사용하여 데이터베이스 연결을 생성합니다.mongodb://myMongo.example.netURI를 대상 클러스터의 연결 문자열 URI로 바꿉니다.encryptedClient = Mongo( "mongodb://myMongo.example.net:27017/?replSetName=myMongo", autoEncryptionOpts )
데이터 암호화
Retrieve the ClientEncryption object and use the ClientEncryption.encrypt() method to encrypt a value using a specific data encryption key UUID and encryption algorithm:
const eDB = "encrypted" const eKV = "__keyVault" const clientEncryption = encryptedClient.getClientEncryption(); const keyVaultClient = Mongo().getDB(eDB).getCollection(eKV) const dek = keyVaultClient.findOne({ keyAltNames: "dataKey1" }) clientEncryption.encrypt( dek._id, "123-45-6789", "Unindexed" )
필드가 포함된 문서를 사용하여 알고리즘을 지정할 수도 있습니다.
algorithmqueryTypecontentionFactor
const eDB = "encrypted" const eKV = "__keyVault" const clientEncryption = encryptedClient.getClientEncryption(); const keyVaultClient = Mongo().getDB(eDB).getCollection(eKV) const dek = keyVaultClient.findOne({ keyAltNames: "dataKey1" }) clientEncryption.encrypt( dek._id, "123-45-6789", { algorithm: "Indexed", queryType: "equality", contentionFactor: 8 } )
결과 예시
If successful, encrypt() returns the encrypted value:
Binary(Buffer.from("05b100000005640020000000005ab3581a43e39a8e855b1ac87013e841735c09d19ae86535eea718dd56122ba50573002000000000703d2cba9832d90436c6c92eb232aa5b968cdcd7a3138570bc87ef0a9eb3a0e905630020000000009cb61df010b1bb54670a5ad979f25f4c48889059dfd8920782cf03dd27d1a50b05650020000000003f5acea703ea357d3eea4c6a5b19139a580089341424a247839fd4d5cf0d312a12636d00040000000000000000", "hex"), 6)
자세히 알아보기
클라이언트 사이드 필드 레벨 암호화가 활성화된 상태에서 MongoDB 연결을 시작하는 방법에 대한 전체 설명서는 Mongo() 를 참조하세요.