关于加密密钥
几个 REST API 端点允许您在 GitHub 上创建密钥。要使用这些端点,您必须使用 libsodium 加密密钥值。更多信息,请参见libsodium 文档。
要加密密钥,您需要一个 Base64 编码的公钥。您可以从 REST API 获取公钥。要确定使用哪个端点获取公钥,请查看您将用于创建密钥的端点中 `encrypted_value` 参数的文档。
使用 Node.js 加密密钥示例
如果您使用的是 Node.js,您可以使用 libsodium-wrappers 库加密您的密钥。更多信息,请参见libsodium-wrappers。
在以下示例中,将 `YOUR_SECRET` 替换为您要加密的纯文本值。将 `YOUR_BASE64_KEY` 替换为您 Base64 编码的公钥。您将用于创建密钥的端点的文档将告诉您可以使用哪个端点获取公钥。`ORIGINAL` 不是占位符;它是 libsodium-wrappers 库的参数。
const sodium = require('libsodium-wrappers')
const secret = 'YOUR_SECRET'
const key = 'YOUR_BASE64_KEY'
//Check if libsodium is ready and then proceed.
sodium.ready.then(() => {
  // Convert the secret and key to a Uint8Array.
  let binkey = sodium.from_base64(key, sodium.base64_variants.ORIGINAL)
  let binsec = sodium.from_string(secret)
  // Encrypt the secret using libsodium
  let encBytes = sodium.crypto_box_seal(binsec, binkey)
  // Convert the encrypted Uint8Array to Base64
  let output = sodium.to_base64(encBytes, sodium.base64_variants.ORIGINAL)
  // Print the output
  console.log(output)
});
const sodium = require('libsodium-wrappers')
const secret = 'YOUR_SECRET'
const key = 'YOUR_BASE64_KEY'
//Check if libsodium is ready and then proceed.
sodium.ready.then(() => {
  // Convert the secret and key to a Uint8Array.
  let binkey = sodium.from_base64(key, sodium.base64_variants.ORIGINAL)
  let binsec = sodium.from_string(secret)
  // Encrypt the secret using libsodium
  let encBytes = sodium.crypto_box_seal(binsec, binkey)
  // Convert the encrypted Uint8Array to Base64
  let output = sodium.to_base64(encBytes, sodium.base64_variants.ORIGINAL)
  // Print the output
  console.log(output)
});
使用 Python 加密密钥示例
如果您使用的是 Python 3,您可以使用 PyNaCl 库加密您的密钥。更多信息,请参见PyNaCl。
在以下示例中,将 `YOUR_SECRET` 替换为您要加密的纯文本值。将 `YOUR_BASE64_KEY` 替换为您 Base64 编码的公钥。您将用于创建密钥的端点的文档将告诉您可以使用哪个端点获取公钥。
from base64 import b64encode
from nacl import encoding, public
def encrypt(public_key: str, secret_value: str) -> str:
  """Encrypt a Unicode string using the public key."""
  public_key = public.PublicKey(public_key.encode("utf-8"), encoding.Base64Encoder())
  sealed_box = public.SealedBox(public_key)
  encrypted = sealed_box.encrypt(secret_value.encode("utf-8"))
  return b64encode(encrypted).decode("utf-8")
encrypt("YOUR_BASE64_KEY", "YOUR_SECRET")
from base64 import b64encode
from nacl import encoding, public
def encrypt(public_key: str, secret_value: str) -> str:
  """Encrypt a Unicode string using the public key."""
  public_key = public.PublicKey(public_key.encode("utf-8"), encoding.Base64Encoder())
  sealed_box = public.SealedBox(public_key)
  encrypted = sealed_box.encrypt(secret_value.encode("utf-8"))
  return b64encode(encrypted).decode("utf-8")
encrypt("YOUR_BASE64_KEY", "YOUR_SECRET")
使用 C# 加密密钥示例
如果您使用的是 C#,您可以使用 Sodium.Core 包加密您的密钥。更多信息,请参见Sodium.Core。
在以下示例中,将 `YOUR_SECRET` 替换为您要加密的纯文本值。将 `YOUR_BASE64_KEY` 替换为您 Base64 编码的公钥。您将用于创建密钥的端点的文档将告诉您可以使用哪个端点获取公钥。
var secretValue = System.Text.Encoding.UTF8.GetBytes("YOUR_SECRET");
var publicKey = Convert.FromBase64String("YOUR_BASE64_KEY");
var sealedPublicKeyBox = Sodium.SealedPublicKeyBox.Create(secretValue, publicKey);
Console.WriteLine(Convert.ToBase64String(sealedPublicKeyBox));
var secretValue = System.Text.Encoding.UTF8.GetBytes("YOUR_SECRET");
var publicKey = Convert.FromBase64String("YOUR_BASE64_KEY");
var sealedPublicKeyBox = Sodium.SealedPublicKeyBox.Create(secretValue, publicKey);
Console.WriteLine(Convert.ToBase64String(sealedPublicKeyBox));
使用 Ruby 加密密钥示例
如果您使用的是 Ruby,您可以使用 RbNaCl gem 加密您的密钥。更多信息,请参见RbNaCl。
在以下示例中,将 `YOUR_SECRET` 替换为您要加密的纯文本值。将 `YOUR_BASE64_KEY` 替换为您 Base64 编码的公钥。您将用于创建密钥的端点的文档将告诉您可以使用哪个端点获取公钥。
require "rbnacl"
require "base64"
key = Base64.decode64("YOUR_BASE64_KEY")
public_key = RbNaCl::PublicKey.new(key)
box = RbNaCl::Boxes::Sealed.from_public_key(public_key)
encrypted_secret = box.encrypt("YOUR_SECRET")
# Print the base64 encoded secret
puts Base64.strict_encode64(encrypted_secret)
require "rbnacl"
require "base64"
key = Base64.decode64("YOUR_BASE64_KEY")
public_key = RbNaCl::PublicKey.new(key)
box = RbNaCl::Boxes::Sealed.from_public_key(public_key)
encrypted_secret = box.encrypt("YOUR_SECRET")
# Print the base64 encoded secret
puts Base64.strict_encode64(encrypted_secret)