Sfoglia il codice sorgente

* Add EncryptedStream using old broken_base64

Bryan Drewery 17 anni fa
parent
commit
52a67c71cd
3 ha cambiato i file con 78 aggiunte e 0 eliminazioni
  1. 44 0
      src/EncryptedStream.c
  2. 33 0
      src/EncryptedStream.h
  3. 1 0
      src/Makefile.in

+ 44 - 0
src/EncryptedStream.c

@@ -0,0 +1,44 @@
+/* EncryptedStream.c
+ *
+ */
+#include "base64.h"
+#include <bdlib/src/String.h>
+#include "EncryptedStream.h"
+#include <stdarg.h>
+#include "compat/compat.h"
+
+int EncryptedStream::gets (char *_data, size_t maxSize) {
+  size_t size = bd::Stream::gets(_data, maxSize);
+  if (key.length()) {
+    bd::String tmp(_data, size);
+    if (tmp[tmp.length() - 1] == '\n')
+      --tmp;
+    bd::String decrypted(decrypt_string(key, broken_base64Decode(tmp)));
+    decrypted += '\n';
+    strlcpy(_data, decrypted.c_str(), maxSize);
+    return decrypted.length();
+  }
+  return size;
+}
+
+void EncryptedStream::printf (const char* format, ...)
+{
+  char va_out[1024] = "";
+  va_list va;
+  size_t len = 0;
+
+  va_start(va, format);
+  len = vsnprintf(va_out, sizeof(va_out), format, va);
+  va_end(va);
+
+  bd::String string(va_out, len);
+  if (key.length()) {
+    if (string[string.length() - 1] == '\n')
+      --string;
+    bd::String encrypted(broken_base64Encode(encrypt_string(key, string)));
+    encrypted += '\n';
+    bd::Stream::puts(encrypted);
+    return;
+  }
+  bd::Stream::puts(string);
+}

+ 33 - 0
src/EncryptedStream.h

@@ -0,0 +1,33 @@
+#ifndef _ENCRYPTEDSTREAM_H
+#define _ENCRYPTEDSTREAM_H 1
+
+namespace bd {
+  class String;
+}
+#include <iostream>
+#include <bdlib/src/Stream.h>
+
+class EncryptedStream : public bd::Stream {
+  private:
+        bd::String key;
+
+  protected:
+
+  public:
+        EncryptedStream(const char* keyStr) : Stream(), key(bd::String(keyStr)) {};
+        EncryptedStream(bd::String& keyStr) : Stream(), key(keyStr) {};
+        EncryptedStream(EncryptedStream& stream) : Stream(stream), key(stream.key) {};
+
+        virtual int gets(char *, size_t);
+#ifdef __GNUC__
+        /* GNU GCC DOC:
+           Since non-static C++ methods have an implicit this argument, the arguments of such methods
+           should be counted from two, not one, when giving values for string-index and first-to-check.
+         */
+        virtual void printf(const char*, ...) __attribute__ ((format(printf, 2, 3)));
+#else
+	virtual void printf(const char*, ...);
+#endif
+
+};
+#endif

+ 1 - 0
src/Makefile.in

@@ -31,6 +31,7 @@ OBJS = auth.o \
 	debug.o \
 	egg_timer.o \
 	enclink.o \
+	EncryptedStream.o \
 	flags.o \
 	garble.o \
 	hash_table.o \