Mercurial > projects > dcrypt
view dcrypt/misc/Util.d @ 21:ec23779ee794
Removed redundant test vector from Blowfish unittest.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Sat, 10 Jan 2009 13:15:14 -0500 |
parents | cd376996cdb3 |
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; } }