Loading sll/trunk/cypher.cc 0 → 100644 +79 −0 Original line number Diff line number Diff line /* This file is part of SLL. Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net> SLL is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. SLL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with SLL; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "slm.hh" #include "cypher.hh" #include <openssl/sha.h> #include <openssl/bio.h> #include <openssl/buffer.h> #include <openssl/evp.h> #include <openssl/md5.h> /** * return sha1 of data * * @param data data to sha1 and base64 * @param size size of data * * @return malloc'ed sha1 size */ unsigned char *Cypher::sha1(char *data, size_t size) { unsigned char *md = new unsigned char[SHA_DIGEST_LENGTH]; // compute sha1 SHA1((const unsigned char *) data, size, md); return md; } /** * return sha1 of data with base64 encoding * * @param data data to sha1 and base64 * @param size size of data * * @return malloc'ed sha1 size */ char *Cypher::sha1_64(char *data, size_t size) { unsigned char md[SHA_DIGEST_LENGTH]; char *buf; BIO *bmem, *b64; BUF_MEM *bptr; // compute sha1 SHA1((const unsigned char *) data, size, md); // compute b64 b64 = BIO_new(BIO_f_base64()); bmem = BIO_new(BIO_s_mem()); b64 = BIO_push(b64, bmem); BIO_write(b64, md, SHA_DIGEST_LENGTH); BIO_flush(b64); BIO_get_mem_ptr(b64, &bptr); if (bptr->length > 0) { buf2 = new char[bptr->length]; memcpy(buf, bptr->data, bptr->length - 1); buf[bptr->length - 1] = 0; } BIO_free_all(b64); return buf; } sll/trunk/cypher.hh 0 → 100644 +31 −0 Original line number Diff line number Diff line /* This file is part of SLL. Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net> SLL is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. SLL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with SLL; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef CYPHER_HH # define CYPHER_HH # include <stddef.h> class Cypher { public: unsigned char *sha1(char *, size_t); unsigned char *sha1_64(char *, size_t); protected: }; #endif Loading
sll/trunk/cypher.cc 0 → 100644 +79 −0 Original line number Diff line number Diff line /* This file is part of SLL. Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net> SLL is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. SLL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with SLL; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "slm.hh" #include "cypher.hh" #include <openssl/sha.h> #include <openssl/bio.h> #include <openssl/buffer.h> #include <openssl/evp.h> #include <openssl/md5.h> /** * return sha1 of data * * @param data data to sha1 and base64 * @param size size of data * * @return malloc'ed sha1 size */ unsigned char *Cypher::sha1(char *data, size_t size) { unsigned char *md = new unsigned char[SHA_DIGEST_LENGTH]; // compute sha1 SHA1((const unsigned char *) data, size, md); return md; } /** * return sha1 of data with base64 encoding * * @param data data to sha1 and base64 * @param size size of data * * @return malloc'ed sha1 size */ char *Cypher::sha1_64(char *data, size_t size) { unsigned char md[SHA_DIGEST_LENGTH]; char *buf; BIO *bmem, *b64; BUF_MEM *bptr; // compute sha1 SHA1((const unsigned char *) data, size, md); // compute b64 b64 = BIO_new(BIO_f_base64()); bmem = BIO_new(BIO_s_mem()); b64 = BIO_push(b64, bmem); BIO_write(b64, md, SHA_DIGEST_LENGTH); BIO_flush(b64); BIO_get_mem_ptr(b64, &bptr); if (bptr->length > 0) { buf2 = new char[bptr->length]; memcpy(buf, bptr->data, bptr->length - 1); buf[bptr->length - 1] = 0; } BIO_free_all(b64); return buf; }
sll/trunk/cypher.hh 0 → 100644 +31 −0 Original line number Diff line number Diff line /* This file is part of SLL. Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net> SLL is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. SLL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with SLL; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef CYPHER_HH # define CYPHER_HH # include <stddef.h> class Cypher { public: unsigned char *sha1(char *, size_t); unsigned char *sha1_64(char *, size_t); protected: }; #endif