COSA
An Object-Oriented Platform for Arduino Programming
RC4.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_RC4_HH
22 #define COSA_RC4_HH
23 
24 #include "Cosa/Types.h"
25 
32 class RC4 {
33 public:
39  RC4(const void* key, size_t len)
40  {
41  restart(key, len);
42  }
43 
49  void restart(const void* key, size_t len);
50 
56  char encrypt(char c)
57  {
58  m_x += 1;
59  uint8_t sx = m_state[m_x];
60  m_y += sx;
61  uint8_t sy = m_state[m_y];
62  m_state[m_x] = sy;
63  m_state[m_y] = sx;
64  uint8_t ix = sx + sy;
65  return (c ^ m_state[ix]);
66  }
67 
73  void encrypt(void* buf, size_t n)
74  {
75  for (char* bp = (char*) buf; n--; bp++)
76  *bp = encrypt(*bp);
77  }
78 
85  void encrypt(void* dest, const void* src, size_t n)
86  {
87  char* dp = (char*) dest;
88  const char* sp = (const char*) src;
89  while (n--) *dp++ = encrypt(*sp++);
90  }
91 
97  char decrypt(char c)
98  {
99  return (encrypt(c));
100  }
101 
107  void decrypt(void* buf, size_t n)
108  {
109  for (char* bp = (char*) buf; n--; bp++)
110  *bp = decrypt(*bp);
111  }
112 
119  void decrypt(void* dest, const void* src, size_t n)
120  {
121  char* dp = (char*) dest;
122  const char* sp = (const char*) src;
123  while (n--) *dp++ = decrypt(*sp++);
124  }
125 
126 private:
127  uint8_t m_state[256];
128  uint8_t m_x;
129  uint8_t m_y;
130 };
131 
132 #endif
RC4(const void *key, size_t len)
Definition: RC4.hh:39
char decrypt(char c)
Definition: RC4.hh:97
Definition: RC4.hh:32
void encrypt(void *dest, const void *src, size_t n)
Definition: RC4.hh:85
void restart(const void *key, size_t len)
Definition: RC4.cpp:24
void decrypt(void *dest, const void *src, size_t n)
Definition: RC4.hh:119
void decrypt(void *buf, size_t n)
Definition: RC4.hh:107
char encrypt(char c)
Definition: RC4.hh:56
void encrypt(void *buf, size_t n)
Definition: RC4.hh:73