Skip to content

Instantly share code, notes, and snippets.

@barrysteyn
Last active September 24, 2024 04:37
Show Gist options
  • Save barrysteyn/7308212 to your computer and use it in GitHub Desktop.
Save barrysteyn/7308212 to your computer and use it in GitHub Desktop.

Revisions

  1. barrysteyn revised this gist May 24, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Base64Decode.c
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ int Base64Decode(char* b64message, unsigned char** buffer, size_t* length) { //D

    int decodeLen = calcDecodeLength(b64message);
    *buffer = (unsigned char*)malloc(decodeLen + 1);
    *(buffer)[decodeLen] = '\0';
    (*buffer)[decodeLen] = '\0';

    bio = BIO_new_mem_buf(b64message, -1);
    b64 = BIO_new(BIO_f_base64());
  2. barrysteyn revised this gist May 24, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Base64Decode.c
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ int Base64Decode(char* b64message, unsigned char** buffer, size_t* length) { //D

    int decodeLen = calcDecodeLength(b64message);
    *buffer = (unsigned char*)malloc(decodeLen + 1);
    *(buffer+decodeLen) = '\0';
    *(buffer)[decodeLen] = '\0';

    bio = BIO_new_mem_buf(b64message, -1);
    b64 = BIO_new(BIO_f_base64());
  3. barrysteyn revised this gist May 24, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Base64Decode.c
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,7 @@ int Base64Decode(char* b64message, unsigned char** buffer, size_t* length) { //D

    int decodeLen = calcDecodeLength(b64message);
    *buffer = (unsigned char*)malloc(decodeLen + 1);
    *(buffer+decodeLen) = '\0';

    bio = BIO_new_mem_buf(b64message, -1);
    b64 = BIO_new(BIO_f_base64());
  4. barrysteyn revised this gist May 24, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Base64Decode.c
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ int Base64Decode(char* b64message, unsigned char** buffer, size_t* length) { //D
    BIO *bio, *b64;

    int decodeLen = calcDecodeLength(b64message);
    *buffer = (unsigned char*)malloc(decodeLen);
    *buffer = (unsigned char*)malloc(decodeLen + 1);

    bio = BIO_new_mem_buf(b64message, -1);
    b64 = BIO_new(BIO_f_base64());
  5. barrysteyn revised this gist May 12, 2015. 2 changed files with 3 additions and 3 deletions.
    4 changes: 2 additions & 2 deletions Base64Decode.c
    Original file line number Diff line number Diff line change
    @@ -18,11 +18,11 @@ size_t calcDecodeLength(const char* b64input) { //Calculates the length of a dec
    return (len*3)/4 - padding;
    }

    int Base64Decode(char* b64message, uint8_t** buffer, size_t* length) { //Decodes a base64 encoded string
    int Base64Decode(char* b64message, unsigned char** buffer, size_t* length) { //Decodes a base64 encoded string
    BIO *bio, *b64;

    int decodeLen = calcDecodeLength(b64message);
    *buffer = (uint8_t*)malloc(decodeLen);
    *buffer = (unsigned char*)malloc(decodeLen);

    bio = BIO_new_mem_buf(b64message, -1);
    b64 = BIO_new(BIO_f_base64());
    2 changes: 1 addition & 1 deletion Base64Encode.c
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    #include <openssl/buffer.h>
    #include <stdint.h>

    int Base64Encode(const uint8_t* buffer, size_t length, char** b64text) { //Encodes a binary safe base 64 string
    int Base64Encode(const unsigned char* buffer, size_t length, char** b64text) { //Encodes a binary safe base 64 string
    BIO *bio, *b64;
    BUF_MEM *bufferPtr;

  6. barrysteyn renamed this gist May 12, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. barrysteyn revised this gist May 12, 2015. 3 changed files with 3 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Base64Decode.c
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    //Decodes Base64
    #include <stdio.h>
    #include <string.h>
    #include <openssl/bio.h>
    #include <openssl/evp.h>
    #include <stdint.h>
    1 change: 1 addition & 0 deletions Main.c
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    #include <stdio.h>
    #include <string.h>

    int main() {
    //Encode To Base64
    File renamed without changes.
  8. barrysteyn revised this gist Apr 13, 2015. 1 changed file with 25 additions and 1 deletion.
    26 changes: 25 additions & 1 deletion Base64.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,28 @@
    OpenSSL Base64 Encoding: Binary Safe and Portable
    =================================================

    Herewith is an example of encoding to and from base64 using OpenSSL's C library. Code presented here is both binary safe, and portable (i.e. it should work on any Posix compliant system e.g. FreeBSD and Linux).
    Herewith is an example of encoding to and from base64 using OpenSSL's C library. Code presented here is both binary safe, and portable (i.e. it should work on any Posix compliant system e.g. FreeBSD and Linux).

    License
    =======
    The MIT License (MIT)

    Copyright (c) 2013 Barry Steyn

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
  9. barrysteyn revised this gist Apr 9, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Base64Decode.c
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ size_t calcDecodeLength(const char* b64input) { //Calculates the length of a dec
    else if (b64input[len-1] == '=') //last char is =
    padding = 1;

    return (size_t)len*0.75 - padding;
    return (len*3)/4 - padding;
    }

    int Base64Decode(char* b64message, uint8_t** buffer, size_t* length) { //Decodes a base64 encoded string
  10. barrysteyn renamed this gist Dec 26, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  11. barrysteyn renamed this gist Dec 26, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  12. barrysteyn renamed this gist Dec 26, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  13. barrysteyn renamed this gist Dec 15, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  14. barrysteyn revised this gist Dec 15, 2013. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    OpenSSL Base64 Encoding: Binary Safe and Portable
    =================================================

    Herewith is an example of encoding to and from base64 using OpenSSL's C library. Code presented here is both binary safe, and portable (i.e. it should work on any Posix compliant system e.g. FreeBSD and Linux).
  15. barrysteyn revised this gist Nov 5, 2013. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion Base64Decode.c
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    #include <stdint.h>
    #include <assert.h>

    size_t calcDecodeLength(const char* b64input) { //Calculates the length of a decoded base64 string
    size_t calcDecodeLength(const char* b64input) { //Calculates the length of a decoded string
    size_t len = strlen(b64input),
    padding = 0;

    2 changes: 1 addition & 1 deletion Base64Encode.c
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    #include <openssl/buffer.h>
    #include <stdint.h>

    int Base64Encode(const uint8_t* buffer, size_t length, char** b64text) { //Encodes a string to base64
    int Base64Encode(const uint8_t* buffer, size_t length, char** b64text) { //Encodes a binary safe base 64 string
    BIO *bio, *b64;
    BUF_MEM *bufferPtr;

  16. barrysteyn revised this gist Nov 5, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Base64Decode.c
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    #include <openssl/bio.h>
    #include <openssl/evp.h>
    #include <stdint.h>
    #include <assert.h>

    size_t calcDecodeLength(const char* b64input) { //Calculates the length of a decoded base64 string
    size_t len = strlen(b64input),
    @@ -27,6 +28,7 @@ int Base64Decode(char* b64message, uint8_t** buffer, size_t* length) { //Decodes

    BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Do not use newlines to flush buffer
    *length = BIO_read(bio, *buffer, strlen(b64message));
    assert(*length == decodeLen); //length should equal decodeLen, else something went horribly wrong
    BIO_free_all(bio);

    return (0); //success
  17. barrysteyn created this gist Nov 4, 2013.
    33 changes: 33 additions & 0 deletions Base64Decode.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    //Decodes Base64
    #include <openssl/bio.h>
    #include <openssl/evp.h>
    #include <stdint.h>

    size_t calcDecodeLength(const char* b64input) { //Calculates the length of a decoded base64 string
    size_t len = strlen(b64input),
    padding = 0;

    if (b64input[len-1] == '=' && b64input[len-2] == '=') //last two chars are =
    padding = 2;
    else if (b64input[len-1] == '=') //last char is =
    padding = 1;

    return (size_t)len*0.75 - padding;
    }

    int Base64Decode(char* b64message, uint8_t** buffer, size_t* length) { //Decodes a base64 encoded string
    BIO *bio, *b64;

    int decodeLen = calcDecodeLength(b64message);
    *buffer = (uint8_t*)malloc(decodeLen);

    bio = BIO_new_mem_buf(b64message, -1);
    b64 = BIO_new(BIO_f_base64());
    bio = BIO_push(b64, bio);

    BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Do not use newlines to flush buffer
    *length = BIO_read(bio, *buffer, strlen(b64message));
    BIO_free_all(bio);

    return (0); //success
    }
    25 changes: 25 additions & 0 deletions Base64Encode.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    //Encodes Base64
    #include <openssl/bio.h>
    #include <openssl/evp.h>
    #include <openssl/buffer.h>
    #include <stdint.h>

    int Base64Encode(const uint8_t* buffer, size_t length, char** b64text) { //Encodes a string to base64
    BIO *bio, *b64;
    BUF_MEM *bufferPtr;

    b64 = BIO_new(BIO_f_base64());
    bio = BIO_new(BIO_s_mem());
    bio = BIO_push(b64, bio);

    BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Ignore newlines - write everything in one line
    BIO_write(bio, buffer, length);
    BIO_flush(bio);
    BIO_get_mem_ptr(bio, &bufferPtr);
    BIO_set_close(bio, BIO_NOCLOSE);
    BIO_free_all(bio);

    *b64text=(*bufferPtr).data;

    return (0); //success
    }
    17 changes: 17 additions & 0 deletions Main.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    #include <stdio.h>

    int main() {
    //Encode To Base64
    char* base64EncodeOutput, *text="Hello World";

    Base64Encode(text, strlen(text), &base64EncodeOutput);
    printf("Output (base64): %s\n", base64EncodeOutput);

    //Decode From Base64
    char* base64DecodeOutput;
    size_t test;
    Base64Decode("SGVsbG8gV29ybGQ=", &base64DecodeOutput, &test);
    printf("Output: %s %d\n", base64DecodeOutput, test);

    return(0);
    }
    2 changes: 2 additions & 0 deletions MakeFile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    all:
    gcc -o base64 Main.c Base64Encode.c Base64Decode.c -lcrypto -lm -w