Variable defaultConst

default: {
    decrypt(recipient: PrivateKey, ciphertextEncoded: string | ArrayBuffer, format: string, options: DecryptionOptions): string | ArrayBuffer;
    decryptToString(recipient: PrivateKey, ciphertextEncoded: string | ArrayBuffer, format: string, options: DecryptionOptions): string;
    encrypt(recipient: PublicKey, plaintextEncoded: string | ArrayBuffer, format: string, options: EncryptionOptions): string | ArrayBuffer;
    encryptFromString(recipient: PublicKey, message: string, format: string, options: EncryptionOptions): string | ArrayBuffer;
    sign(signer: PrivateKey, hash: string, plaintextEncoded: string | ArrayBuffer, format: string, options: SigningOptions): string | ArrayBuffer;
    signString(signer: PrivateKey, hash: string, plaintext: string, format: string, options: SigningOptions): string | ArrayBuffer;
    verify(verifier: PublicKey, hash: string, plaintextEncoded: string | ArrayBuffer, signatureEncoded: string | ArrayBuffer, options: VerifyingOptions): boolean;
    verifyString(verifier: PublicKey, hash: string, plaintext: string, signatureEncoded: string | ArrayBuffer, options: VerifyingOptions): boolean;
}

Type declaration

  • decrypt:function
    • 解密。

      import x509 from 'pts/crypto/x509';
      import crypto from 'pts/crypto';
      import util from 'pts/util';

      const publicKeyPem = open("./ca.pem");
      const privateKeyPem = open("./ca.key.pem");

      let publicKey = x509.parseCertificateToPublicKey(publicKeyPem);
      let privateKey = x509.parsePrivateKey(privateKeyPem);

      export default function() {
      let data = "hello world";
      let outputEncoding = "base64";
      let options = {
      type: "oaep",
      hash: "sha256",
      };
      let dataEncoded = util.base64Encoding(data);

      let encrypted = crypto.encrypt(publicKey, dataEncoded, outputEncoding, options);
      let decrypted = crypto.decrypt(privateKey, encrypted, outputEncoding, options);

      // aGVsbG8gd29ybGQ=
      console.log(decrypted);
      }

      Parameters

      • recipient: PrivateKey

        公钥

      • ciphertextEncoded: string | ArrayBuffer

        需要解密的内容,可以是原始数据,也可以是 hex 或 base64 编码后的数据

      • format: string

        输出格式,默认为 ArrayBuffer,可选 base64 和 hex,返回编码后的字符串

      • options: DecryptionOptions

        解密选项

      Returns string | ArrayBuffer

      解密的结果

  • decryptToString:function
    • 解密。

      import x509 from 'pts/crypto/x509';
      import crypto from 'pts/crypto';
      import util from 'pts/util';

      const publicKeyPem = open("./ca.pem");
      const privateKeyPem = open("./ca.key.pem");

      let publicKey = x509.parseCertificateToPublicKey(publicKeyPem);
      let privateKey = x509.parsePrivateKey(privateKeyPem);

      export default function() {
      let data = "hello world";
      let outputEncoding = "base64";
      let options = {
      type: "oaep",
      hash: "sha256",
      };
      let dataEncoded = util.base64Encoding(data);

      let encrypted = crypto.encrypt(publicKey, dataEncoded, outputEncoding, options);
      let decrypted = crypto.decryptToString(privateKey, encrypted, options);

      // hello world
      console.log(decrypted);
      }

      Parameters

      • recipient: PrivateKey

        公钥

      • ciphertextEncoded: string | ArrayBuffer

        需要解密的内容,可以是原始数据,也可以是 hex 或 base64 编码后的数据

      • format: string

        输出格式,默认为 ArrayBuffer,可选 base64 和 hex,返回编码后的字符串

      • options: DecryptionOptions

        解密选项

      Returns string

      解密的结果

  • encrypt:function
    • 加密。

      import x509 from 'pts/crypto/x509';
      import crypto from 'pts/crypto';
      import util from 'pts/util';

      const publicKeyPem = open("./ca.pem");
      const privateKeyPem = open("./ca.key.pem");

      let publicKey = x509.parseCertificateToPublicKey(publicKeyPem);
      let privateKey = x509.parsePrivateKey(privateKeyPem);

      export default function() {
      let data = "hello world";
      let outputEncoding = "base64";
      let options = {
      type: "oaep",
      hash: "sha256",
      };
      let dataEncoded = util.base64Encoding(data);

      let encrypted = crypto.encrypt(publicKey, dataEncoded, outputEncoding, options);
      let decrypted = crypto.decryptToString(privateKey, encrypted, options);

      // hello world
      console.log(decrypted);
      }

      Parameters

      • recipient: PublicKey

        公钥

      • plaintextEncoded: string | ArrayBuffer

        需要加密的内容,可以是原始数据,也可以是 hex 或 base64 编码后的数据

      • format: string

        输出格式,默认为 ArrayBuffer,可选 base64 和 hex,返回编码后的字符串

      • options: EncryptionOptions

        加密选项

      Returns string | ArrayBuffer

      加密后的内容

  • encryptFromString:function
    • 加密。

      import x509 from 'pts/crypto/x509';
      import crypto from 'pts/crypto';

      const publicKeyPem = open("./ca.pem");
      const privateKeyPem = open("./ca.key.pem");

      let publicKey = x509.parseCertificateToPublicKey(publicKeyPem);
      let privateKey = x509.parsePrivateKey(privateKeyPem);

      export default function() {
      let data = "hello world";
      let outputEncoding = "base64";
      let options = {
      type: "oaep",
      hash: "sha256",
      };

      let encrypted = crypto.encryptFromString(publicKey, data, outputEncoding, options);
      let decrypted = crypto.decryptToString(privateKey, encrypted, options);

      // hello world
      console.log(decrypted);
      }

      Parameters

      • recipient: PublicKey

        公钥

      • message: string

        需要加密的内容

      • format: string

        输出格式,默认为 ArrayBuffer,可选 base64 和 hex,返回编码后的字符串

      • options: EncryptionOptions

        加密选项

      Returns string | ArrayBuffer

      加密后的内容

  • sign:function
    • 签名。

      import x509 from 'pts/crypto/x509';
      import crypto from 'pts/crypto';
      import util from 'pts/util';

      const publicKeyPem = open("./ca.pem");
      const privateKeyPem = open("./ca.key.pem");

      let publicKey = x509.parseCertificateToPublicKey(publicKeyPem);
      let privateKey = x509.parsePrivateKey(privateKeyPem);

      export default function () {
      let message = "This is a message.";
      let messageEncoded = util.base64Encoding(message);
      let options = {};

      let signed = crypto.sign(privateKey, "sha256", messageEncoded, "base64", options);
      let verified = crypto.verify(publicKey, "sha256", messageEncoded, signed, options);
      // true
      console.log(verified);
      }

      Parameters

      • signer: PrivateKey

        私钥

      • hash: string

        哈希算法,默认为 sha256,支持算法的包括 md5 sha1 sha256 sha512 sha3_256 sha3_512 等

      • plaintextEncoded: string | ArrayBuffer

        需要签名的内容,可以是原始数据 ArrayBuffer,也可以是 hex 或 base64 编码后的字符串

      • format: string

        输出格式,默认为 ArrayBuffer,可选 base64 和 hex,返回编码后的字符串

      • options: SigningOptions

        签名选项

      Returns string | ArrayBuffer

      签名结果

  • signString:function
    • 签名。

      import x509 from 'pts/crypto/x509';
      import crypto from 'pts/crypto';
      import util from 'pts/util';

      const publicKeyPem = open("./ca.pem");
      const privateKeyPem = open("./ca.key.pem");

      let publicKey = x509.parseCertificateToPublicKey(publicKeyPem);
      let privateKey = x509.parsePrivateKey(privateKeyPem);

      export default function () {
      let message = "This is a message.";
      let messageEncoded = util.base64Encoding(message);
      let options = {};

      let signed = crypto.signString(privateKey, "sha256", message, "base64", options);
      let verified = crypto.verify(publicKey, "sha256", messageEncoded, signed, options);
      // true
      console.log(verified);
      }

      Parameters

      • signer: PrivateKey

        私钥

      • hash: string

        哈希算法,默认为 sha256,支持算法的包括 md5 sha1 sha256 sha512 sha3_256 sha3_512 等

      • plaintext: string

        需要签名的内容,原始数据字符串

      • format: string

        输出格式,默认为 ArrayBuffer,可选 base64 和 hex,返回编码后的字符串

      • options: SigningOptions

        签名选项

      Returns string | ArrayBuffer

      签名结果

  • verify:function
    • 验签。

      import x509 from 'pts/crypto/x509';
      import crypto from 'pts/crypto';
      import util from 'pts/util';

      const publicKeyPem = open("./ca.pem");
      const privateKeyPem = open("./ca.key.pem");

      let publicKey = x509.parseCertificateToPublicKey(publicKeyPem);
      let privateKey = x509.parsePrivateKey(privateKeyPem);

      export default function () {
      let message = "This is a message.";
      let messageEncoded = util.base64Encoding(message);
      let options = {};

      let signed = crypto.sign(privateKey, "sha256", messageEncoded, "base64", options);
      let verified = crypto.verify(publicKey, "sha256", messageEncoded, signed, options);
      // true
      console.log(verified);
      }

      Parameters

      • verifier: PublicKey

        公钥

      • hash: string

        哈希算法,默认为 sha256,支持算法的包括 md5 sha1 sha256 sha512 sha3_256 sha3_512 等

      • plaintextEncoded: string | ArrayBuffer

        被签名的内容,可以是原始数据 ArrayBuffer,也可以是 hex 或 base64 编码后的字符串

      • signatureEncoded: string | ArrayBuffer

        需要验证的签名结果,可以是 ArrayBuffer,也可以是 hex 或 base64 编码后的字符串

      • options: VerifyingOptions

        验签选项

      Returns boolean

      验签结果

  • verifyString:function
    • 验签。

      import x509 from 'pts/crypto/x509';
      import crypto from 'pts/crypto';
      import util from 'pts/util';

      const publicKeyPem = open("./ca.pem");
      const privateKeyPem = open("./ca.key.pem");

      let publicKey = x509.parseCertificateToPublicKey(publicKeyPem);
      let privateKey = x509.parsePrivateKey(privateKeyPem);

      export default function () {
      let message = "This is a message.";
      let messageEncoded = util.base64Encoding(message);
      let options = {};

      let signed = crypto.sign(privateKey, "sha256", messageEncoded, "base64", options);
      let verified = crypto.verifyString(publicKey, "sha256", message, signed, options);
      // true
      console.log(verified);
      }

      Parameters

      • verifier: PublicKey

        公钥

      • hash: string

        哈希算法,默认为 sha256,支持算法的包括 md5 sha1 sha256 sha512 sha3_256 sha3_512 等

      • plaintext: string

        被签名的内容,原始数据字符串

      • signatureEncoded: string | ArrayBuffer

        需要验证的签名结果,可以是 ArrayBuffer,也可以是 hex 或 base64 编码后的字符串

      • options: VerifyingOptions

        验签选项

      Returns boolean

      验签结果