Class

class ECDSAPrivateKey

public class ECDSAPrivateKey <: PrivateKey {
    public init(curve: Curve)
}

Description: Indicates the ECDSA private key class which provides the capability of generating ECDSA private keys. ECDSA private keys support sign operations as well as encoding and decoding in PEM and DER formats.

Parent Type:

init()

public init(curve: Curve)

Description: Generates a private key with initialization performed.

Parameters:

  • curve: Curve: elliptic curve type

Throws:

func decodeDer(DerBlob)

public static func decodeDer(blob: DerBlob): ECDSAPrivateKey

Description: Decodes a private key from the DER format.

Parameters:

  • blob: DerBlob: private key object in binary format

Returns:

Throws:

func decodeDer(DerBlob, ?String)

public static func decodeDer(blob: DerBlob, password!: ?String): ECDSAPrivateKey

Description: Decodes an encrypted private key from the DER format.

Parameters:

  • blob: DerBlob: private key object in binary format
  • password!: ?String: password required for decrypting the private key. If the password is None, decryption is not performed.

Returns:

Throws:

  • CryptoException: If decoding or decryption fails, or the parameter password is empty, this exception is thrown.

func decodeFromPem(String)

public static func decodeFromPem(text: String): ECDSAPrivateKey

Description: Decodes a private key from the PEM format.

Parameters:

  • text: String: private key character stream in PEM format

Returns:

Throws:

  • CryptoException: If decoding fails, the character stream does not comply with the PEM format, or the file header does not comply with the private key header standard, this exception is thrown.

func decodeFromPem(String, ?String)

public static func decodeFromPem(text: String, password!: ?String): ECDSAPrivateKey

Description: Decodes a private key from the PEM format.

Parameters:

  • text: String: private key character stream in PEM format
  • password!: ?String: password required for decrypting the private key. If the password is None, decryption is not performed.

Returns:

Throws:

  • CryptoException: If decoding or decryption fails, the parameter password is empty, the character stream does not comply with the PEM format, or the file header does not comply the private key header standard, this exception is thrown.

func encodeToDer()

public override func encodeToDer(): DerBlob

Description: Encodes a private key in DER format.

Returns:

  • DerBlob: encoded private key in DER format

Throws:

func encodeToDer(?String)

public func encodeToDer(password!: ?String): DerBlob

Description: Encrypts a private key using AES-256-CBC and encodes the private key in DER format.

Parameters:

  • password!: ?String: password required for encrypting the private key. If the password is None, encryption is not performed.

Returns:

  • DerBlob: encoded private key in DER format

Throws:

  • CryptoException: If encoding or encryption fails, or the parameter password is empty, this exception is thrown.

func encodeToPem()

public override func encodeToPem(): PemEntry

Description: Encodes a private key in PEM format.

Returns:

  • PemEntry: object of the private key in PEM format

Throws:

func sign(Array<Byte>)

public func sign(digest: Array<Byte>): Array<Byte>

Description: Signs the digest result of data.

Parameters:

  • digest: Array<Byte>: digest result of the data

Returns:

Throws:

func toString

public override func toString(): String

Description: Outputs the private key type.

Returns:

  • String: private key type description

class ECDSAPublicKey

public class ECDSAPublicKey <: PublicKey {
    public init(pri: ECDSAPrivateKey)
}

Description: Indicates the ECDSA public key class which provides the capability of generating ECDSA public keys. ECDSA public keys support signature authentication as well as encoding and decoding in PEM and DER formats.

Parent Type:

init(ECDSAPrivateKey)

public init(pri: ECDSAPrivateKey)

Description: Initializes a public key with initialization performed, that is, obtains the corresponding public key from a private key.

Parameters:

Throws:

func decodeDer(DerBlob)

public static func decodeDer(blob: DerBlob): ECDSAPublicKey

Description: Decodes a public key from the DER format.

Parameters:

  • blob: DerBlob: public key object in binary format

Returns:

Throws:

func decodeFromPem(String)

public static func decodeFromPem(text: String): ECDSAPublicKey

Description: Decodes a public key from the PEM format.

Parameters:

  • text: String: public key character stream in PEM format

Returns:

Throws:

  • CryptoException: If decoding fails, the character stream does not comply with the PEM format, or the file header does not comply with the public key header standard, this exception is thrown.

func encodeToDer()

public override func encodeToDer(): DerBlob

Description: Encodes a public key in DER format.

Returns:

  • DerBlob: encoded public key in DER format

Throws:

func encodeToPem()

public override func encodeToPem(): PemEntry

Description: Encodes a public key in PEM format.

Returns:

Throws:

func toString

public override func toString(): String

Description: Outputs the public key type.

Returns:

  • String: private key type description

func verify(Array<Byte>, Array<Byte>)

public func verify(digest: Array<Byte>, sig: Array<Byte>): Bool

Description: Verifies the signing result.

Parameters:

  • digest: Array<Byte>: digest result of the data
  • sig: Array<Byte>: signing result of the data

Returns:

  • Bool: If true is returned, the verification is successful. If false is returned, the verification fails.

class RSAPrivateKey

public class RSAPrivateKey <: PrivateKey{
    public init(bits: Int32)
    public init(bits: Int32, e: BigInt)
}

Description: Indicates the RSA private key class which provides the capability of generating RSA private keys. RSA private keys support sign and decrypt operations as well as encoding and decoding in PEM and DER formats, complying with the PKCS1 standard.

Parent Type:

init(Int32)

public init(bits: Int32)

Description: Generates a private key with initialization performed. The default public key exponent is 65537, which is recommended in the industry. The public key exponent e directly affects the security and encryption efficiency of the RSA algorithm. Generally, a smaller value of e indicates a higher encryption speed but lower security.

Parameters:

  • bits: Int32: key length, which must be greater than or equal to 512 bits and less than or equal to 16,384 bits

Throws:

  • CryptoException: If the key length does not meet the requirements or the initialization fails, this exception is thrown.

init(Int32, BigInt)

public init(bits: Int32, e: BigInt)

Description: Generates a private key with initialization performed and allowing users to specify the public exponent.

Parameters:

  • bits: Int32: key length, which must be greater than 512 bits and less than or equal to 16,384 bits; recommended key length: no less than 3,072 bits
  • e: BigInt: public exponent of the public key; value range: odd number in [3, 2^256 – 1].

Throws:

  • CryptoException: If the key length or the public exponent of the public key does not meet the requirements, or the initialization fails, this exception is thrown.

func decodeDer(DerBlob)

public static func decodeDer(blob: DerBlob): RSAPrivateKey

Description: Decodes a private key from the DER format.

Parameters:

  • blob: DerBlob: private key object in binary format

Returns:

Throws:

func decodeDer(DerBlob, ?String)

public static func decodeDer(blob: DerBlob, password!: ?String): RSAPrivateKey

Description: Decodes an encrypted private key from the DER format.

Parameters:

  • blob: DerBlob: private key object in binary format
  • password!: ?String: password required for decrypting the private key. If the password is None, decryption is not performed.

Returns:

Throws:

  • CryptoException: If decoding or decryption fails, or the parameter password is empty, this exception is thrown.

func decodeFromPem(String)

public static func decodeFromPem(text: String): RSAPrivateKey

Description: Decodes a private key from the PEM format.

Parameters:

  • text: String: private key character stream in PEM format

Returns:

Throws:

  • CryptoException: If decoding or decryption fails, the character stream does not comply with the PEM format, or the file header does not comply with the private key header standard, this exception is thrown.

func decodeFromPem(String, ?String)

public static func decodeFromPem(text: String, password!: ?String): RSAPrivateKey

Description: Decodes a private key from the PEM format.

Parameters:

  • text: String: private key character stream in PEM format
  • password!: ?String: password required for decrypting the private key. If the password is None, decryption is not performed.

Returns:

Throws:

  • CryptoException: If decoding or decryption fails, the parameter password is empty, the character stream does not comply with the PEM format, or the file header does not comply the private key header standard, this exception is thrown.

func decrypt(InputStream, OutputStream, PadOption)

public func decrypt(input: InputStream, output: OutputStream, padType!: PadOption): Unit

Description: Performs decryption to obtain the original data.

Parameters:

  • input: InputStream: encrypted data
  • output: OutputStream: data after decryption
  • padType!: PadOption: padding mode. The PKCS1 or OAEP mode can be selected. The PSS mode is not supported. The OAEP mode is recommended in scenarios with high security requirements.

Throws:

  • CryptoException: When the padding mode fails to be set or the decryption fails, this exception is thrown.

func encodeToDer()

public override func encodeToDer(): DerBlob

Description: Encodes a private key in DER format.

Returns:

  • DerBlob: encoded private key in DER format

Throws:

func encodeToDer(?String)

public func encodeToDer(password!: ?String): DerBlob

Description: Encrypts a private key using AES-256-CBC and encodes the private key in DER format.

Parameters:

  • password!: ?String: password required for encrypting the private key. If the password is None, encryption is not performed.

Returns:

  • DerBlob: encoded private key in DER format

Throws:

  • CryptoException: If encoding or encryption fails, or the parameter password is empty, this exception is thrown.

func encodeToPem()

public override func encodeToPem(): PemEntry

Description: Encodes a private key in PEM format.

Returns:

  • PemEntry: object of the private key in PEM format

Throws:

func sign(Digest, Array<Byte>, PadOption)

public func sign(hash: Digest, digest: Array<Byte>, padType!: PadOption): Array<Byte>

Description: Signs the digest result of data.

Parameters:

  • hash: Digest: digest method used to obtain the digest result
  • digest: Array<Byte>: digest result of the data
  • padType!: PadOption: padding mode. The PKCS1 or PSS mode can be selected. The OAEP mode is not supported. The PSS mode is recommended in scenarios with high security requirements.

Returns:

Throws:

  • CryptoException: When the digest method or padding mode fails to be set, or signing fails, this exception is thrown.

func toString()

public override func toString(): String

Description: Outputs the private key type.

Returns:

  • String: private key type description

class RSAPublicKey

public class RSAPublicKey <: PublicKey {
    public init(pri: RSAPrivateKey)
}

Description: Indicates the RSA public key class which provides the capability of generating RSA public keys. RSA public keys support signature authentication and encryption as well as encoding and decoding in PEM and DER formats.

Parent Type:

init(RSAPrivateKey)

public init(pri: RSAPrivateKey)

Description: Initializes a public key with initialization performed, that is, obtains the corresponding public key from a private key.

Parameters:

Throws:

func decodeDer(DerBlob)

public static func decodeDer(blob: DerBlob): RSAPublicKey

Description: Decodes a public key from the DER format.

Parameters:

  • blob: DerBlob: public key object in binary format

Returns:

Throws:

func decodeFromPem(String)

public static func decodeFromPem(text: String): RSAPublicKey

Description: Decodes a public key from the PEM format.

Parameters:

  • text: String: public key character stream in PEM format

Returns:

Throws:

  • CryptoException: If decoding fails, the character stream does not comply with the PEM format, or the file header does not comply with the public key header standard, this exception is thrown.

func encodeToDer()

public override func encodeToDer(): DerBlob

Description: Encodes a public key in DER format.

Returns:

  • DerBlob: encoded public key in DER format

Throws:

func encodeToPem()

public override func encodeToPem(): PemEntry

Description: Encodes a public key in PEM format.

Returns:

Throws:

func encrypt(InputStream, OutputStream, PadOption)

public func encrypt(input: InputStream, output: OutputStream, padType!: PadOption): Unit

Description: Encrypts a segment of data.

Parameters:

  • input: InputStream: data to be encrypted
  • output: OutputStream: data after encryption
  • padType!: PadOption: padding mode. The PKCS1 or OAEP mode can be selected. The PSS mode is not supported. The OAEP mode is recommended in scenarios with high security requirements.

Throws:

  • CryptoException: When the padding mode fails to be set or the encryption fails, this exception is thrown.

func toString()

public override func toString(): String

Description: Outputs the public key type.

Returns:

  • String: private key type description

func verify(Digest, Array<Byte>, Array<Byte>, PadOption)

public func verify(hash: Digest, digest: Array<Byte>, sig: Array<Byte>, padType!: PadOption): Bool

Description: Verifies the signing result.

Parameters:

  • hash: Digest: digest method used to obtain the digest result
  • digest: Array<Byte>: digest result of the data
  • sig: Array<Byte>: signing result of the data
  • padType!: PadOption: padding mode. The PKCS1 or PSS mode can be selected. The OAEP mode is not supported. The PSS mode is recommended in scenarios with high security requirements.

Returns:

  • Bool: If true is returned, the verification is successful. If false is returned, the verification fails.

Throws:

  • CryptoException: When the padding mode fails to be set or the authentication fails, this exception is thrown.

class SM2PrivateKey

public class SM2PrivateKey <: PrivateKey {
    public init()
}

Description: Indicates the SM2 private key class which provides the capability of generating SM2 private keys. SM2 private keys support sign and decrypt operations as well as encoding and decoding in PEM and DER formats, complying with the PKCS1 standard.

Parent Type:

init()

public init()

Description: Generates a private key with initialization performed.

Throws:

func decodeDer(DerBlob)

public static func decodeDer(blob: DerBlob): SM2PrivateKey

Description: Decodes a private key from the DER format.

Parameters:

  • blob: DerBlob: private key object in binary format

Returns:

Throws:

func decodeDer(DerBlob, ?String)

public static func decodeDer(blob: DerBlob, password!: ?String): SM2PrivateKey

Description: Decodes an encrypted private key from the DER format.

Parameters:

  • blob: DerBlob: private key object in binary format
  • password!: ?String: password required for decrypting the private key. If the password is None, decryption is not performed.

Returns:

Throws:

  • CryptoException: If decoding or decryption fails, or the parameter password is empty, this exception is thrown.

func decodeFromPem(String)

public static func decodeFromPem(text: String): SM2PrivateKey

Description: Decodes a private key from the PEM format.

Parameters:

  • text: String: private key character stream in PEM format

Returns:

Throws:

  • CryptoException: If decoding or decryption fails, the character stream does not comply with the PEM format, or the file header does not comply with the private key header standard, this exception is thrown.

func decodeFromPem(String, ?String)

public static func decodeFromPem(text: String, password!: ?String): SM2PrivateKey

Description: Decodes a private key from the PEM format.

Parameters:

  • text: String: private key character stream in PEM format
  • password!: ?String: password required for decrypting the private key. If the password is None, decryption is not performed.

Returns:

Throws:

  • CryptoException: If decoding or decryption fails, the parameter password is empty, the character stream does not comply with the PEM format, or the file header does not comply the private key header standard, this exception is thrown.

func decrypt(Array<Byte>)

public func decrypt(input: Array<Byte>): Array<Byte>

Description: Performs decryption to obtain the original data.

Parameters:

Returns:

Throws:

func encodeToDer()

public func encodeToDer(): DerBlob

Description: Encodes a private key in DER format.

Returns:

  • DerBlob: encoded private key in DER format

Throws:

func encodeToDer(?String)

public func encodeToDer(password!: ?String): DerBlob

Description: Encrypts a private key using AES-256-CBC and encodes the private key in DER format.

Parameters:

  • password!: ?String: password required for encrypting the private key. If the password is None, encryption is not performed.

Returns:

  • DerBlob: encoded public key in DER format

Throws:

  • CryptoException: If encoding or encryption fails, or the parameter password is empty, this exception is thrown.

func encodeToPem(?String)

public func encodeToPem(password!: ?String): PemEntry 

Description: Encodes an encrypted private key in PEM format.

Parameters:

  • password!: ?String: password required for encrypting the private key. If the password is None, encryption is not performed.

Returns:

  • PemEntry: object of the private key in PEM format

Throws:

  • CryptoException: If encoding or encryption fails, or the parameter password is empty, this exception is thrown.

func encodeToPem()

public func encodeToPem(): PemEntry

Description: Encodes a private key in PEM format.

Returns:

  • PemEntry: object of the private key in PEM format

Throws:

func sign(Array<Byte>)

public func sign(data: Array<Byte>): Array<Byte>

Description: Signs data. SM2 uses the SM3 data digest algorithm.

Parameters:

Returns:

Throws:

func toString

public override func toString(): String

Description: Outputs the private key type.

Returns:

  • String: private key type description

class SM2PublicKey

public class SM2PublicKey <: PublicKey {
    public init(pri: SM2PrivateKey)
}

Description: Indicates the SM2 public key class which provides the capability of generating SM2 public keys. SM2 public keys support signature authentication and encryption as well as encoding and decoding in PEM and DER formats.

Parent Type:

init(SM2PrivateKey)

public init(pri: SM2PrivateKey)

Description: Initializes a public key with initialization performed, that is, obtains the corresponding public key from a private key.

Parameters:

Throws:

func decodeDer(DerBlob)

public static func decodeDer(blob: DerBlob): SM2PublicKey

Description: Decodes a public key from the DER format.

Parameters:

  • blob: DerBlob: public key object in binary format

Returns:

Throws:

func decodeFromPem(String)

public static func decodeFromPem(text: String): SM2PublicKey

Description: Decodes a public key from the PEM format.

Parameters:

  • text: String: public key character stream in PEM format

Returns:

Throws:

  • CryptoException: If decoding fails, the character stream does not comply with the PEM format, or the file header does not comply with the public key header standard, this exception is thrown.

func encodeToDer()

public func encodeToDer(): DerBlob

Description: Encodes a public key in DER format.

Returns:

  • DerBlob: encoded public key in DER format

Throws:

func encodeToPem()

public func encodeToPem(): PemEntry

Description: Encodes a public key in PEM format.

Returns:

Throws:

func encrypt(Array<Byte>)

public func encrypt(input: Array<Byte>): Array<Byte> 

Description: Encrypts a segment of data.

Parameters:

Returns:

Throws:

func toString()

public override func toString(): String

Description: Outputs the public key type.

Returns:

  • String: private key type description

func verify(Array<Byte>, Array<Byte>)

public func verify(data: Array<Byte>, sig: Array<Byte>): Bool

Description: Verifies the signing result.

Parameters:

Returns:

  • Bool: If true is returned, the verification is successful. If false is returned, the verification fails.

Throws:

  • CryptoException: When the padding mode fails to be set or the authentication fails, this exception is thrown.