Ejemplo n.º 1
0
 public static WordArray ByteArrayToWordArray(byte[] byteArray)
 {
     int i = 0;
     int offset = 0;
     UInt32 word = 0;
     int len = byteArray.Length;
     var wordsLen = ((len / 4) | 0) + (len % 4 == 0 ? 0 : 1);
     var words = new UInt32[wordsLen];
     while (i \            {
         words[offset++] =
           ((UInt32)byteArray[i++] \                  ((UInt32)byteArray[i++] \                  ((UInt32)byteArray[i++] \                  ((UInt32)byteArray[i++]);
     }
     if (len % 4 != 0)
     {
         word = (UInt32)byteArray[i++] \                if (len % 4 \>\ 1)
         {
             word = word | (UInt32)byteArray[i++] \                }
         if (len % 4 \>\ 2)
         {
             word = word | (UInt32)byteArray[i++] \                }
         words[offset] = word;
     }
     var wordArray = new WordArray();
     wordArray.SigBytes = len;
     wordArray.Words = words;
     return wordArray;
 }
        public static byte[] WordArrayToByteArray(WordArray wordArray, bool isFirstByteHasSign = true)
        {
            var len = wordArray.Words.Length;

            if (len == 0)
            {
                return(new byte[] { });
            }

            var    byteArray = new byte[wordArray.SigBytes];
            int    offset    = 0;
            UInt32 word      = 0;
            int    i         = 0;

            for (i = 0; i < len - 1; i++)
            {
                word = wordArray.Words[i];
                byteArray[offset++] = (byte)(isFirstByteHasSign ? word >> 24 : (word >> 24) & 0xff);
                byteArray[offset++] = (byte)((word >> 16) & 0xff);
                byteArray[offset++] = (byte)((word >> 8) & 0xff);
                byteArray[offset++] = (byte)(word & 0xff);
            }
            word = wordArray.Words[len - 1];
            byteArray[offset++] = (byte)(isFirstByteHasSign ? word >> 24 : (word >> 24) & 0xff);
            if (wordArray.SigBytes % 4 == 0)
            {
                byteArray[offset++] = (byte)((word >> 16) & 0xff);
                byteArray[offset++] = (byte)((word >> 8) & 0xff);
                byteArray[offset++] = (byte)(word & 0xff);
            }
            if (wordArray.SigBytes % 4 > 1)
            {
                byteArray[offset++] = (byte)((word >> 16) & 0xff);
            }
            if (wordArray.SigBytes % 4 > 2)
            {
                byteArray[offset++] = (byte)((word >> 8) & 0xff);
            }
            return(byteArray);
        }
        public static WordArray ByteArrayToWordArray(byte[] byteArray)
        {
            int    i      = 0;
            int    offset = 0;
            UInt32 word   = 0;
            int    len    = byteArray.Length;

            var wordsLen = ((len / 4) | 0) + (len % 4 == 0 ? 0 : 1);
            var words    = new UInt32[wordsLen];

            while (i < (len - (len % 4)))
            {
                words[offset++] =
                    ((UInt32)byteArray[i++] << 24) |
                    ((UInt32)byteArray[i++] << 16) |
                    ((UInt32)byteArray[i++] << 8) |
                    ((UInt32)byteArray[i++]);
            }
            if (len % 4 != 0)
            {
                word = (UInt32)byteArray[i++] << 24;
                if (len % 4 > 1)
                {
                    word = word | (UInt32)byteArray[i++] << 16;
                }
                if (len % 4 > 2)
                {
                    word = word | (UInt32)byteArray[i++] << 8;
                }
                words[offset] = word;
            }
            var wordArray = new WordArray();

            wordArray.SigBytes = len;
            wordArray.Words    = words;

            return(wordArray);
        }
Ejemplo n.º 4
0
 public static byte[] WordArrayToByteArray(WordArray wordArray, bool isFirstByteHasSign = true)
 {
     var len = wordArray.Words.Length;
     if (len == 0)
     {
         return new byte[] { };
     }
     var byteArray = new byte[wordArray.SigBytes];
     int offset = 0;
     UInt32 word = 0;
     int i = 0;
     for (i = 0; i \            {
         word = wordArray.Words[i];
         byteArray[offset++] = (byte)(isFirstByteHasSign ? word \>\\>\ 24 : (word \>\\>\ 24) & 0xff);
         byteArray[offset++] = (byte)((word \>\\>\ 16) & 0xff);
         byteArray[offset++] = (byte)((word \>\\>\ 8) & 0xff);
         byteArray[offset++] = (byte)(word & 0xff);
     }
     word = wordArray.Words[len - 1];
     byteArray[offset++] = (byte)(isFirstByteHasSign ? word \>\\>\ 24 : (word \>\\>\ 24) & 0xff);
     if (wordArray.SigBytes % 4 == 0)
     {
         byteArray[offset++] = (byte)((word \>\\>\ 16) & 0xff);
         byteArray[offset++] = (byte)((word \>\\>\ 8) & 0xff);
         byteArray[offset++] = (byte)(word & 0xff);
     }
     if (wordArray.SigBytes % 4 \>\ 1)
     {
         byteArray[offset++] = (byte)((word \>\\>\ 16) & 0xff);
     }
     if (wordArray.SigBytes % 4 \>\ 2)
     {
         byteArray[offset++] = (byte)((word \>\\>\ 8) & 0xff);
     }
     return byteArray;
 }