juno.security.crypto Module

Provides cryptographic services including secure encoding and decoding of data, as well as hashing and random number generation.

Licence
See licence.txt for use and distribution terms.

class CryptoException: object.Exception;

The exception thrown when an error occurs during a cryptogaphic operation.

abstract interface ICryptoTransform;

Defines the basic operations of cryptographic transformations.

abstract uint transformBlock(ubyte[] inputBuffer, uint inputOffset, uint inputCount, ubyte[] outputBuffer, uint outputOffset);

Transforms the specified input array and copies the resulting transform to the specified output array.

abstract ubyte[] transformFinalBlock(ubyte[] inputBuffer, uint inputOffset, uint inputCount);

Transforms the specified array.

abstract uint inputBlockSize();

Gets the input block size.

abstract uint outputBlockSize();

Gets the output block size.

enum CipherMode;

Specifies the block cipher mode to use for encryption.

CBC

Cipher Block Chaining.

ECB

Electronic Codebook.

OFB

Output Feedback.

CFB

Cipher Feedback.

CTS

Cipher Text Stealing.

enum PaddingMode;

Specifies the type of padding to apply when the message data block is shorter than the full number of bytes needed for a cryptographic operation.

None

No padding is done.

PKCS7



Zeros



ANSIX923



ISO10126



enum CryptoStreamMode;

Specifies the mode of a cryptographic stream.

Read

Read access to a cryptographic stream.

Write

Write access to a cryptographic stream.

class CryptoStream: std.stream.Stream;

Defines a stream that links data streams to cryptographic transformations.

this(Stream stream, ICryptoTransform transform, CryptoStreamMode mode);

Initializes a new instance.

final void flushFinalBlock();

Updates the underlying data source with the current state of the buffer.

abstract class HashAlgorithm: juno.security.crypto.ICryptoTransform, juno.base.core.IDisposable;

Represents the base class from which implementations of cryptographic hash algorithms derive.

final void clear();

Releases all resources held by this instance.

final ubyte[] computeHash(ubyte[] buffer, uint offset, uint count);
final ubyte[] computeHash(ubyte[] buffer);
final ubyte[] computeHash(Stream input);

Computes the hash for the specified data.

final uint transformBlock(ubyte[] inputBuffer, uint inputOffset, uint inputCount, ubyte[] outputBuffer, uint outputOffset);

Computes the hash value for the specified input array and copies the resulting hash value to the specified output array.

final ubyte[] transformFinalBlock(ubyte[] inputBuffer, uint inputOffset, uint inputCount);

Computes the hash value for the specified input array.

ubyte[] hash();

Gets the value of the computed hash code.

uint hashSize();

Gets the size in bits of the computed hash code.

uint inputBlockSize();

Gets the input block size.

uint outputBlockSize();

Gets the output block size.

class KeySizes;

Determines the set of valid key sizes for symmetric cryptographic algorithms.

this(uint min, uint max, uint skip);

Initializes a new instance.

uint min();

Specifies the minimum key size in bits.

uint max();

Specifies the maximum key size in bits.

uint skip();

Specifies the interval between valid key sizes in bits.

abstract class SymmetricAlgorithm: juno.base.core.IDisposable;

The abstract base class from which implementations of symmetric algorithms derive.

abstract ICryptoTransform createEncryptor(ubyte[] key, ubyte[] iv);
ICryptoTransform createEncryptor();

Creates a symmetric encryptor object.

abstract ICryptoTransform createDecryptor(ubyte[] key, ubyte[] iv);
ICryptoTransform createDecryptor();

Creates a symmetric decryptor object.

abstract void generateKey();

Generates a random key to use for the algorithm.

abstract void generateIV();

Generates a random initialization vector to use for the algorithm.

final bool isValidKeySize(uint bitLength);

Determines whether the specified key size is valid for the algorithm.

void key(ubyte[] value);
ubyte[] key();

Gets or sets the secret key for the symmetric algorithm.

void keySize(uint value);
uint keySize();

Gets or sets the size in bits of the secret key used by the symmetric algorithm.

KeySizes[] legalKeySizes();

Gets the key sizes in bits supported by the symmetric algorithm.

void iv(ubyte[] value);
ubyte[] iv();

Gets or sets the initialization vector for the symmetric algorithm.

void blockSize(uint value);
uint blockSize();

Gets or sets the block size in bits of the symmetric algorithm.

void mode(CipherMode value);
CipherMode mode();

Gets or sets the mode of operation of the symmetric algorithm.

void padding(PaddingMode value);
PaddingMode padding();

Gets or sets the padding mode used in the symmetric algorithm.

abstract class AsymmetricAlgorithm: juno.base.core.IDisposable;

The abstract base class from which implementations of asymmetric algorithms derive.

void keySize(uint value);
uint keySize();

Gets or sets the size in bits of the key used by the asymmetric algorithm.

KeySizes[] legalKeySizes();

Gets the key sizes supported by the asymmetric algorithm.

abstract char[] keyExchangeAlgorithm();

Gets the name of the key exchange algorithm.

abstract char[] signatureAlgorithm();

Gets the name of the signature algorithm.

abstract class RandomNumberGenerator;

Represents the abstract base class from which implementations of cryptographic random number generators derive.

abstract void getBytes(ubyte[] data);

Fills an array with a a cryptographically strong random sequence of values.

abstract void getNonZeroBytes(ubyte[] data);

Fills an array with a a cryptographically strong random sequence of non-zero values.

abstract class Md5: juno.security.crypto.HashAlgorithm;

The base class from which implementations of the MD5 hash algorithms derive.

class Md5CryptoServiceProvider: juno.security.crypto.Md5;

Computes the MD5 hash value for the input data using the implementation provided by the cryptographic service provider.

Examples
 import juno.base.text, juno.security.crypto, std.stdio;

 void main() {
   string text = "Some text to be hashed";
   ubyte[] textBytes = Encoding.UTF8.encode(text);

   scope md5 = new MD5CryptoServiceProvider;
   ubyte[] hashedBytes = md5.computeHash(textBytes);

   // Writes out the hashed text as r+ITAIu+cM+Csl1GW5qYSQ==
   string hashedText = std.base64.encode(hashedBytes);
   writefln(hashedText);
 }

class Md5Cng: juno.security.crypto.Md5;

Provides a CNG (Cryptography Next Generation) implementation of the MD5 hashing algorithm.

abstract class Sha1: juno.security.crypto.HashAlgorithm;

Computes the SHA1 hash value for the input data.

class Sha1CryptoServiceProvider: juno.security.crypto.Sha1;

Computes the SHA1 hash value for the input data using the implementation provided by the cryptographic service provider.

class Sha1Cng: juno.security.crypto.Sha1;

Provides a CNG (Cryptography Next Generation) implementation of the Secure Hash Algorithm (SHA).

abstract class Sha256: juno.security.crypto.HashAlgorithm;

Computes the SHA256 hash value for the input data.

class Sha256CryptoServiceProvider: juno.security.crypto.Sha256;

Computes the SHA256 hash value for the input data using the implementation provided by the cryptographic service provider.

class Sha256Cng: juno.security.crypto.Sha256;

Provides a CNG (Cryptography Next Generation) implementation of the Secure Hash Algorithm (SHA) for 256-bit hash values.

abstract class Sha384: juno.security.crypto.HashAlgorithm;

Computes the SHA384 hash value for the input data.

class Sha384CryptoServiceProvider: juno.security.crypto.Sha384;

Computes the SHA384 hash value for the input data using the implementation provided by the cryptographic service provider.

class Sha384Cng: juno.security.crypto.Sha384;

Provides a CNG (Cryptography Next Generation) implementation of the Secure Hash Algorithm (SHA) for 384-bit hash values.

abstract class Sha512: juno.security.crypto.HashAlgorithm;

Computes the SHA512 hash value for the input data.

class Sha512CryptoServiceProvider: juno.security.crypto.Sha512;

Computes the SHA512 hash value for the input data using the implementation provided by the cryptographic service provider.

class Sha512Cng: juno.security.crypto.Sha512;

Provides a CNG (Cryptography Next Generation) implementation of the Secure Hash Algorithm (SHA) for 512-bit hash values.

abstract class TripleDes: juno.security.crypto.SymmetricAlgorithm;

The base class from which implementations of the Triple DES algorithms derive.

class TripleDesCryptoServiceProvider: juno.security.crypto.TripleDes;

Provides access to the cryptographic service provider implementation of the Triple DES algorithm.

abstract class Aes: juno.security.crypto.SymmetricAlgorithm;

The base class from which implementations of the Advanced Encryption Standard (AES) derive.

class AesCryptoServiceProvider: juno.security.crypto.Aes;

Provides access to the cryptographic service provider implementation of the AES algorithm.

Examples
 import juno.security.crypto, juno.base.text, std.stdio;

 ubyte[] encryptText(string text, ubyte[] key, ubyte[] iv) {
   scope aes = new AesCryptoServiceProvider;

   scope ms = new MemoryStream;
   scope cs = new CryptoStream(ms, aes.createEncryptor(key, iv), CryptoStreamMode.Write);

   ubyte[] data = Encoding.UTF8.encode(text);

   cs.write(data);
   cs.flushFinalBlock();

   ubyte[] ret = ms.data;

   cs.close();
   ms.close();

   return ret;
 }

 ubyte[] decryptText(ubyte[] data, ubyte[] key, ubyte[] iv) {
   scope aes = new AesCryptoServiceProvider;

   scope ms = new MemoryStream(data);
   scope cs = new CryptoStream(ms, aes.createEncryptor(key, iv), CryptoStreamMode.Read);

   ubyte[] bytes = new ubyte[data.length];

   cs.read(bytes);

   return Encoding.UTF8.decode(bytes);
 }

 void main() {
   string text = "Some text to encrypt.";

   scope aes = new AesCryptoServiceProvider;
   ubyte[] data = encryptText(text, aes.key, aes.iv);

   text = decryptText(data, aes.key, aes.iv);
   writefln(text);
 }

class RngCryptoServiceProvider: juno.security.crypto.RandomNumberGenerator;

Implements a cryptographic random number generator using the implementation provided by the cryptographic service provider.

class RngCng: juno.security.crypto.RandomNumberGenerator;

Provides a CNG (Cryptography Next Generation) implementation of a cryptographic random number generator.

enum DataProtectionScope;



ubyte[] protectData(ubyte[] userData, ubyte[] optionalEntropy, DataProtectionScope protectionScope);



ubyte[] unprotectData(ubyte[] protectedData, ubyte[] optionalEntropy, DataProtectionScope protectionScope);



enum MemoryProtectionScope;



void protectMemory(ubyte[] userData, MemoryProtectionScope protectionScope);



void unprotectMemory(ubyte[] encryptedData, MemoryProtectionScope protectionScope);