view dcrypt/misc/Util.d @ 10:cd376996cdb3

Renamed SymmetricCipher back to Cipher (we don't support any other kind atm, I'll deal with it when we do.). Added BlockCipherWrapper for the encryption of arbitrary streams with or without padding. Removed hashByName, and replaced it with createHash. Re-did the high-level API, and filled out Crypto. Added cipher creation via createCipher. Added dsk to the CONTRIBUTORS file for helping with the design of the high-level API.
author Thomas Dixon <reikon@reikon.us>
date Wed, 20 Aug 2008 20:08:07 -0400
parents 0e08791a1418
children
line wrap: on
line source

/**
 * This file is part of the dcrypt project.
 *
 * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved.
 * License:   MIT
 * Authors:   Thomas Dixon
 */

// TODO: WRITE DOCS 
module dcrypt.misc.Util;

/** Utility functions */
struct Util {
    static const char[] hexits = "0123456789abcdef";
    
    final static uint ubytesToUintBig(ubyte[] x, uint offset) {
        return cast(uint) (((x[offset++] & 0xff) << 24) |
                           ((x[offset++] & 0xff) << 16) |
                           ((x[offset++] & 0xff) << 8)  |
                            (x[offset] & 0xff));
    }
    
    final static uint ubytesToUintLittle(ubyte[] x, uint offset) {
        return cast(uint) ((x[offset++] & 0xff)        |
                          ((x[offset++] & 0xff) << 8)  |
                          ((x[offset++] & 0xff) << 16) |
                          ((x[offset] & 0xff) << 24));
    }
    
    final static ulong ubytesToUlongLittle(ubyte[] x, uint offset) {
        return cast(ulong) ((x[offset++] & 0xff)        |
                           ((x[offset++] & 0xff) << 8)  |
                           ((x[offset++] & 0xff) << 16) |
                           ((x[offset++] & 0xff) << 24) |
                           (cast(ulong)(x[offset++] & 0xff) << 32) |
                           (cast(ulong)(x[offset++] & 0xff) << 40) |
                           (cast(ulong)(x[offset++] & 0xff) << 48) |
                           (cast(ulong)(x[offset] & 0xff) << 56));
    }
    
    final static ulong ubytesToUlongBig(ubyte[] x, uint offset) {
        return cast(ulong) ((cast(ulong)(x[offset++] & 0xff) << 56) |
                            (cast(ulong)(x[offset++] & 0xff) << 48) |
                            (cast(ulong)(x[offset++] & 0xff) << 40) |
                            (cast(ulong)(x[offset++] & 0xff) << 32) |
                            ((x[offset++] & 0xff) << 24) |
                            ((x[offset++] & 0xff) << 16) |
                            ((x[offset++] & 0xff) << 8)  |
                             (x[offset] & 0xff));
    }
    
    final static ushort ubytesToUshortLittle(ubyte[] x, uint offset) {
        return cast(ushort) ((x[offset++] & 0xff)       |
                            ((x[offset] & 0xff) << 8));
    }
    
    final static ushort ubytesToUshortBig(ubyte[] x, uint offset) {
        return cast(ushort) (((x[offset++] & 0xff) << 8) |
                              (x[offset] & 0xff));
    }
    
    final static void uintToUbytesBig(uint x, ubyte[] output, uint outOff) {
        output[outOff++] = cast(ubyte)(x >> 24);
        output[outOff++] = cast(ubyte)(x >> 16);
        output[outOff++] = cast(ubyte)(x >> 8);
        output[outOff] = cast(ubyte)(x);
    }
    
    final static void uintToUbytesLittle(uint x, ubyte[] output, uint outOff) {
        output[outOff++] = cast(ubyte)(x);
        output[outOff++] = cast(ubyte)(x >> 8);
        output[outOff++] = cast(ubyte)(x >> 16);
        output[outOff] = cast(ubyte)(x >> 24);
    }
    
    final static void ulongToUbytesBig(ulong x, ubyte[] output, uint outOff) {
        output[outOff++] = cast(ubyte)(x >> 56);
        output[outOff++] = cast(ubyte)(x >> 48);
        output[outOff++] = cast(ubyte)(x >> 40);
        output[outOff++] = cast(ubyte)(x >> 32);
        output[outOff++] = cast(ubyte)(x >> 24);
        output[outOff++] = cast(ubyte)(x >> 16);
        output[outOff++] = cast(ubyte)(x >> 8);
        output[outOff] = cast(ubyte)(x);
    }
    
    final static void ulongToUbytesLittle(ulong x, ubyte[] output, uint outOff) {
        output[outOff++] = cast(ubyte)(x);
        output[outOff++] = cast(ubyte)(x >> 8);
        output[outOff++] = cast(ubyte)(x >> 16);
        output[outOff++] = cast(ubyte)(x >> 24);
        output[outOff++] = cast(ubyte)(x >> 32);
        output[outOff++] = cast(ubyte)(x >> 40);
        output[outOff++] = cast(ubyte)(x >> 48);
        output[outOff] = cast(ubyte)(x >> 56);
    }
    
    final static uint rotateLeft(uint x, uint y) {
        return (x << y) | (x >> (32-y));
    }
    
    final static ulong rotateLeft64(ulong x, uint y) {
        return (x << y) | (x >> (64-y));
    }
    
    final static uint rotateRight(uint x, uint y) {
        return (x >> y) | (x << (32-y));    
    }
    
    final static ulong rotateRight64(ulong x, uint y) {
        return (x >> y) | (x << (64-y));    
    }
    
    final static char[] ubytesToHex(ubyte[] input) {
        char[] output = new char[input.length<<1];
        int i = 0;
        foreach (ubyte j; input) { 
            output[i++] = hexits[j >> 4];
            output[i++] = hexits[j & 0xf];
        }
        return output;    
    }
    
    final static ubyte[] hexToUbytes(char[] input) {
        ubyte[] output = new ubyte[input.length>>1];
        static ubyte[char] hexitIndex;
        for (int i = 0; i < hexits.length; i++)
            hexitIndex[hexits[i]] = i;
        for (int i = 0, j = 0; i < output.length; i++) {
            output[i] = hexitIndex[input[j++]] << 4;
            output[i] |= hexitIndex[input[j++]]; 
        }
        return output;
    }
    
    final static char[] stringToLower(char[] input) {
        char[] output = new char[input.length];
        foreach (uint i, char c; input) 
            output[i] = ((c >= 'A' && c <= 'Z') ? c+32 : c);
        return output;
    }
    
    final static char[] stringToUpper(char[] input) {
        char[] output = new char[input.length];
        foreach (uint i, char c; input) 
            output[i] = ((c >= 'a' && c <= 'z') ? c-32 : c);
        return output;
    }
    
    final static char[] stringToAlphanumeric(char[] input) {
        char[] output = "";
        foreach (char c; input)
            if ((c >= 'a' && c <= 'z') ||
                (c >= 'A' && c <= 'Z') ||
                (c >= '0' && c <= '9'))
                    output ~= c;
        return output;
    }
}