Module

Xojo.Crypto


Warning

This item was deprecated in version 2020r2. Please use Crypto as a replacement.

Description

This namespace contains hashing methods for use with cryptography.

Methods

Name

Parameters

Returns

Shared

BERDecodePrivateKey

privateKey As MemoryBlock

MemoryBlock

BERDecodePublicKey

publicKey As MemoryBlock

MemoryBlock

DEREncodePrivateKey

privateKey As MemoryBlock

MemoryBlock

DEREncodePublicKey

publicKey As MemoryBlock

MemoryBlock

GenerateRandomBytes

byteCount As UInteger

MemoryBlock

Hash

data As MemoryBlock, algorithm As Xojo.Crypto

MemoryBlock

HMAC

key As MemoryBlock, data As MemoryBlock, algorithm As Xojo.Crypto

MemoryBlock

MD5

data As MemoryBlock

MemoryBlock

PBKDF2

salt As MemoryBlock, data As MemoryBlock, iterations As UInt32, desiredHashLength As UInteger, hashAlgorithm As Xojo.Crypto

MemoryBlock

RSADecrypt

data As MemoryBlock, privateKey As MemoryBlock

MemoryBlock

RSAEncrypt

data As MemoryBlock, publicKey As MemoryBlock

MemoryBlock

RSAGenerateKeyPair

bits As UInteger, ByRef privateKey As MemoryBlock, ByRef publicKey As MemoryBlock

Boolean

RSASign

data As MemoryBlock, privateKey As MemoryBlock

MemoryBlock

RSAVerifyKey

key As MemoryBlock

Boolean

RSAVerifySignature

data As MemoryBlock, signature As MemoryBlock, publicKey As MemoryBlock

Boolean

SHA1

data As MemoryBlock

MemoryBlock

SHA256

data As MemoryBlock

MemoryBlock

SHA512

data As MemoryBlock

MemoryBlock

Enumerations

Xojo.Crypto.HashAlgorithms

HashAlgorithms

Used by the HMAC, Hash and PBKDF2 methods to specify the type of algorithm to use. (MD5, SHA1, SHA256, SHA512)

Enum

Description

MD5

MD5 message-digest algorithm.

SHA1

SHA-1 produces a 160-bit (20-byte) hash value.

SHA256

SHA-2 with 256 bit digest.

SHA512

SHA-2 with 512 bit digest.

Method descriptions


Xojo.Crypto.BERDecodePrivateKey

BERDecodePrivateKey(privateKey As MemoryBlock) As MemoryBlock

Decodes a private key for interoperability with the BER encoding for use by other libraries.

For more information, refer to BER and DER Encoding.


Xojo.Crypto.BERDecodePublicKey

BERDecodePublicKey(publicKey As MemoryBlock) As MemoryBlock

Decodes a public key for interoperability with the BER encoding for use by other libraries.

For more information, refer to BER and DER Encoding.


Xojo.Crypto.DEREncodePrivateKey

DEREncodePrivateKey(privateKey As MemoryBlock) As MemoryBlock

Encodes a private key for interoperability with the DER encoding for use by other libraries.

For more information, refer to BER and DER Encoding.


Xojo.Crypto.DEREncodePublicKey

DEREncodePublicKey(publicKey As MemoryBlock) As MemoryBlock

Encodes a public key for interoperability with the DER encoding for use by other libraries.

For more information, refer to BER and DER Encoding.


Xojo.Crypto.GenerateRandomBytes

GenerateRandomBytes(byteCount As UInteger) As MemoryBlock

Generates a random block of data of the specified byteCount.

Dim mb1 As New Xojo.Core.MemoryBlock(1024)
mb1 = Xojo.Crypto.GenerateRandomBytes(1024)

Xojo.Crypto.Hash

Hash(data As MemoryBlock, algorithm As Xojo.Crypto) As MemoryBlock

Creates a hash value for the data using the specified algorithm.

Using Xojo.Core
Using Xojo.Crypto
Dim hash As MemoryBlock
hash = Hash("YourPasswordSentence", Crypto.HashAlgorithms.SHA512)

Xojo.Crypto.HMAC

HMAC(key As MemoryBlock, data As MemoryBlock, algorithm As Xojo.Crypto) As MemoryBlock

Creates the hash-based message authentication code using the data, the supplied key value and the supplied algorithm.

The key value is applied to the data before generating the hash. Refer to HMAC on wikipedia.


Xojo.Crypto.MD5

MD5(data As MemoryBlock) As MemoryBlock

Generates the MD5 message-digest value of the data.


Xojo.Crypto.PBKDF2

PBKDF2(salt As MemoryBlock, data As MemoryBlock, iterations As UInt32, desiredHashLength As UInteger, hashAlgorithm As Xojo.Crypto) As MemoryBlock

Returns the PBKDF2 hash value of the data, first applying the salt value and using the specified hashAlgorithm. The iterations parameter is the number of loops that the hash algorithm does. The desiredHashLength parameter lets you specify the number of bytes that you want the resulting hash to be. 16 or 32 bytes are commonly used.

PBKDF2 is a "slow", i.e. deliberately processing intensive, algorithm for generating hash values. Slow is relative, for generating a single hash value it is plenty fast. The benefit of a slow algorithm is that it is impractical for hackers to generate hash tables using it because it would take too long to generate the thousands of hashes for commonly used values. Use a higher value for iterations to further slow the hash creation.

Refer to PBKDF2 on Wikipedia.

Using Xojo.Core
Using Xojo.Crypto

Dim salt As Text = "SaltValue"
Dim saltMB As MemoryBlock
saltMB = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(salt)

Dim password As Text = "YourPasswordSentence"
Dim passwordMB As MemoryBlock
passwordMB = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(password)

Dim hash As MemoryBlock
hash = PBKDF2(saltMB, passwordMB, 100, 32, HashAlgorithms.SHA512)

Xojo.Crypto.RSADecrypt

RSADecrypt(data As MemoryBlock, privateKey As MemoryBlock) As MemoryBlock

Decrypts data using the specified privateKey.


Xojo.Crypto.RSAEncrypt

RSAEncrypt(data As MemoryBlock, publicKey As MemoryBlock) As MemoryBlock

Encrypts the data using the specified publicKey.

The data that is being encrypted should be kept pretty short (usually just a couple hundred characters, but it depends on the number of bits you use to create the keys). Typically you use the this to communicate a "secret key" of some kind that can be used to decrypt an actual message that was encrypted using some other technique (such as AES).

Warning

A CryptoException will be raised if you attempt to encrypt using a private key.


Xojo.Crypto.RSAGenerateKeyPair

RSAGenerateKeyPair(bits As UInteger, ByRef privateKey As MemoryBlock, ByRef publicKey As MemoryBlock) As Boolean

Generates a private and public key pair that is hex encoded. True if the keys were successfully generated, False if they were not.

You will typically use a bits value of 1024 or 2048.


Xojo.Crypto.RSASign

RSASign(data As MemoryBlock, privateKey As MemoryBlock) As MemoryBlock

Signs the data block using the specified privateKey using PKCS v1.5 with SHA1.

Sign a message:

Using Xojo.Core
Using Xojo.Crypto

Dim privateKey As MemoryBlock
Dim publicKey As MemoryBlock

If RSAGenerateKeyPair( 1024, privateKey, publicKey ) Then
  ' 1024-bit private and public keys were generated

  Dim msg As MemoryBlock = TextEncoding.UTF8.ConvertTextToData("this is a test")

  Dim signature As MemoryBlock = RSASign( msg, privateKey )
  If signature <> Nil Then
    ' msg was successfully signed
  End If
End If

Xojo.Crypto.RSAVerifyKey

RSAVerifyKey(key As MemoryBlock) As Boolean

Attempts to validate the specified key. True if the signature is valid, False if it is not.


Xojo.Crypto.RSAVerifySignature

RSAVerifySignature(data As MemoryBlock, signature As MemoryBlock, publicKey As MemoryBlock) As Boolean

Verifies the data using the specified signature and key using PKCS v1.5 with SHA1. True if the signature is valid, False if it is not.


Xojo.Crypto.SHA1

SHA1(data As MemoryBlock) As MemoryBlock

Generates the SHA1 hash value for data.


Xojo.Crypto.SHA256

SHA256(data As MemoryBlock) As MemoryBlock

Generates SHA256 hash value for data.

Dim t As Text = "TestData"

' Convert text to a MemoryBlock
Dim textData As Xojo.Core.MemoryBlock
textData = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(t)

' Create SHA256
Dim sha1 As Xojo.Core.MemoryBlock
sha1 = Xojo.Crypto.SHA256(textData)

' Convert SHA256 to Text
' Note that this contains invalid UTF8 characters so really
' cannot be displayed
Dim sha1Text As Text
sha1Text = Xojo.Core.TextEncoding.UTF8.ConvertDataToText(sha1, True)

Xojo.Crypto.SHA512

SHA512(data As MemoryBlock) As MemoryBlock

Generates SHA512 hash value for data.

Compatibility

All project types on all supported operating systems.

See also

MemoryBlock class