AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

ClientEncryption.encrypt() (mongosh 메서드)

ClientEncryption.encrypt(keyId, value, algorithm or encOptions)

ClientEncryption.encrypt() encrypts the value using the specified keyId and the algorithm specified by algorithm or encOptions. encrypt() supports explicit (manual) encryption of field values.

반환합니다:

하위 유형이 6binary data 객체 입니다.

이 명령은 다음 환경에서 호스팅되는 배포에서 사용할 수 있습니다.

  • MongoDB Atlas: 클라우드에서의 MongoDB 배포를 위한 완전 관리형 서비스
clientEncryption = db.getMongo().getClientEncryption()
clientEncryption.encrypt(
keyId,
value,
algorithm or encOptions,
)
Parameter
유형
설명

keyId

UUID

value를 암호화하는 데 사용할 데이터 암호화 키입니다.

The UUID is a BSON binary data object with subtype 4 that identifies a specific data encryption key. If the data encryption key does not exist in the key vault configured for the database connection, encrypt() returns an error. See Key Vault Collections for more information on key vaults and data encryption keys.

value

지원되지 않는 BSON type을 참조하세요.

암호화할 값입니다.

algorithm or encOptions

문자열 또는 문서

  • 클라이언트측 필드 레벨 암호화를 사용하여 필드를 명시적으로 암호화하려면 다음을 수행합니다.

    algorithm을 문자열로 지정하거나 encOptions를 필드 algorithm이 포함된 문서로 지정합니다.

    지원되는 알고리즘은 다음과 같습니다.

    • AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic

    • AEAD_AES_256_CBC_HMAC_SHA_512-Random

    예시는 클라이언트측 필드 레벨 암호화 알고리즘 설정을 참조하세요.

    지원되는 암호화 알고리즘에 대한 전체 문서는 필드 및 암호화 유형을 참조하세요.

  • Queryable Encryption으로 필드를 명시적으로 암호화하려면 다음을 수행합니다.

    algorithm을 문자열로 지정하거나 encOptions를 다음 필드가 포함된 문서로 지정합니다.

    • algorithm: value를 암호화하는 데 사용할 암호화 알고리즘입니다. 지원되는 알고리즘은 다음과 같습니다.

      • Indexed

      • Unindexed

    • contentionFactor: algorithmIndexed로 설정된 경우 필요합니다. 이 필드에 대한 값의 빈도와 관련이 있습니다.

    • queryType: 현재 지원되는 유일한 쿼리 유형은 "equality"입니다. 알고리즘이 Indexed가 아닌 경우 queryType을 설정해야 합니다.

    예시는 Queryable Encryption 알고리즘 설정을 참조하세요.

    지원되는 암호화 알고리즘에 대한 자세한 내용은 알고리즘 선택을 참조하세요.

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:

or

You cannot use encrypt() to encrypt values with the following BSON types:

  • minKey

  • maxKey

  • null

  • undefined

If encrypting a field using AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic, encrypt() does not support the following BSON types:

  • double

  • decimal128

  • bool

  • object

  • array

다음 예에서는 클라이언트 사이드 필드 수준 암호화 구성을 위해 로컬로 managed KMS를 사용합니다.

1
  1. mongosh 시작

    실행:

    mongosh --nodb

    --nodb 데이터베이스 에 연결하지 않음을 의미합니다.

  2. 키 string생성

    기본 64 96바이트 string 을 생성합니다.

    const TEST_LOCAL_KEY = require("crypto").randomBytes(96).toString("base64")
  3. 암호화 옵션 객체 만들기

    클라이언트 사이드 필드 수준 암호화 옵션 객체 를 만들려면 이전 단계의 TEST_LOCAL_KEY string 을 사용합니다.

    var autoEncryptionOpts = {
    "keyVaultNamespace" : "encryption.__dataKeys",
    "kmsProviders" : {
    "local" : {
    "key" : BinData(0, TEST_LOCAL_KEY)
    }
    }
    }
  4. 암호화된 클라이언트 객체 만들기

    암호화됨 클라이언트 객체 를 만들려면 Mongo() 생성자를 사용합니다. mongodb://myMongo.example.net URI를 대상 클러스터 의 연결 string URI 로 바꿉니다. 예를 예시 다음과 같습니다.

    encryptedClient = Mongo(
    "mongodb://myMongo.example.net:27017/?replSetName=myMongo",
    autoEncryptionOpts
    )
2

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 구성을 위해 로컬로 관리되는 KMS를 사용합니다.

1
  1. mongosh 시작

    mongosh 클라이언트를 시작합니다.

    mongosh --nodb
  2. 키 생성

    로컬로 관리되는 키에 대해 Queryable Encryption 을 구성하려면 줄 바꿈 없이 기본64인코딩된 96바이트 string 을 생성합니다.

    const TEST_LOCAL_KEY = require("crypto").randomBytes(96).toString("base64")
  3. Queryable Encryption 옵션 생성

    생성된 로컬 키 문자열을 사용하여 Queryable Encryption 옵션을 생성합니다.

    var autoEncryptionOpts = {
    "keyVaultNamespace" : "encryption.__dataKeys",
    "kmsProviders" : {
    "local" : {
    "key" : BinData(0, TEST_LOCAL_KEY)
    }
    }
    }
  4. 암호화된 클라이언트 만들기

    Queryable Encryption옵션이 구성된 Mongo() 생성자를 사용하여 데이터베이스 연결을 생성합니다. mongodb://myMongo.example.net URI를 대상 클러스터의 연결 문자열 URI로 바꿉니다.

    encryptedClient = Mongo(
    "mongodb://myMongo.example.net:27017/?replSetName=myMongo",
    autoEncryptionOpts
    )
2

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"
)

필드가 포함된 문서를 사용하여 알고리즘을 지정할 수도 있습니다.

  • algorithm

  • queryType

  • contentionFactor

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: 4
}
)

If successful, encrypt() returns the encrypted value:

Binary(Buffer.from("05b100000005640020000000005ab3581a43e39a8e855b1ac87013e841735c09d19ae86535eea718dd56122ba50573002000000000703d2cba9832d90436c6c92eb232aa5b968cdcd7a3138570bc87ef0a9eb3a0e905630020000000009cb61df010b1bb54670a5ad979f25f4c48889059dfd8920782cf03dd27d1a50b05650020000000003f5acea703ea357d3eea4c6a5b19139a580089341424a247839fd4d5cf0d312a12636d00040000000000000000", "hex"), 6)

클라이언트 사이드 필드 레벨 암호화가 활성화된 상태에서 MongoDB 연결을 시작하는 방법에 대한 전체 설명서는 Mongo() 를 참조하세요.

이 페이지 평가하기