Skip to content

caseyscarborough/libcrypt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

libcrypt

libcrypt is a simple library used for encryption, hashing, and encoding in C. It is currently a work in progress.

Using the Library

To use the library, include the crypt.h and crypt.c files in your project. Then, include the crypt.h file at the top of each file you'd like to use it's methods in. See test/crypt-test.c for example usage.

To run the test file or the Base64 converter, run make from the project directory.

Methods

The following methods are available in the libcrypt library. Note that these methods return pre-allocated blocks of memory, and it is up to the user of the method to free the memory after use (shown below).

rot13

This method encodes a string of text using the ROT13 cipher.

char *text = "The Quick Brown Fox Jumps Over The Lazy Dog.";
char *rot13_encoded_text = rot13(text);

printf("%s\n", rot13_encoded_text);
// Outputs: Gur Dhvpx Oebja Sbk Whzcf Bire Gur Ynml Qbt.

free(rot13_encoded_text);

rot47

This method encodes a string of text using the ROT47 cipher.

char *text = "The Quick Brown Fox Jumps Over The Lazy Dog.";
char *rot47_encoded_text = rot13(text);

printf("%s\n", rot47_encoded_text);
// Outputs: %96 "F:4< qC@H? u@I yF>AD ~G6C %96 {2KJ s@8]

free(rot47_encoded_text);

base64_encode

This method encodes text using the Base64 encoding scheme.

char *text = "The Quick Brown Fox Jumps Over The Lazy Dog.";
char *base64_encoded_text = base64_encode(text);
printf("%s\n", base64_encoded_text);
// Outputs: VGhlIFF1aWNrIEJyb3duIEZveCBKdW1wcyBPdmVyIFRoZSBMYXp5IERvZy4=

free(base64_encoded_text);

base64_decode

This method decodes Base64 encoded text.

char *text = "VGhpcyBpcyBzb21lIGR1bW15IHRleHQgdG8gYmUgZW5jb2RlZCBhbmQgZGVjb2RlZC4=";
char *base64_decoded_text = base64_decode(text);
printf("%s\n", base64_decoded_text);
// Outputs: This is some dummy text to be encoded and decoded.

free(base64_decoded_text);

md5

This method hashes a given string using the MD5 hashing algorithm.

char *text = "The quick brown fox jumps over the lazy dog."
char *md5_text = md5(text);
printf("%s\n", md5_text);
// Outputs: e4d909c290d0fb1ca068ffaddf22cbd0

free(md5_text);

generate_uuid

This method This method generates a version 4 Universally Unique Identifier, which is randomly generated.

char *uuid = generate_uuid();
printf("%s\n", uuid);
// Outputs 3586BD54-55C8-42D8-BE0E-74308A5B9D22

free(uuid);

Utilities

Base64 Converter

Included in this library is a Base64 file converter utility, located in the file utils/b64.c. This utility will convert the contents of a file to or from Base64 encoded data.

# Build the executables
$ make && cd bin

# Encode a file
$ ./b64 -e input.txt output.txt

# Decode a file
$ ./b64 -e encoded.txt decoded.txt

# Print the help menu
$ ./b64 -h

MD5 Hasher

The MD5 Hasher utility is located in the file utils/md5.c. This utility will MD5 hash the contents of a file or a string.

$ make && cd bin

# Hash a string
$ ./md5 -s "The quick brown fox jumps over the lazy dog."
e4d909c290d0fb1ca068ffaddf22cbd0

# Hash a file's contents
$ ./md5 -f sample.txt
322a166e1402bd9ff18e1ce67fe69e61  sample.txt

# Print the help menu
$ ./md5 -h

Either of these compiled utilities can then be copied to somewhere in your PATH to be available from anywhere.

Running the Tests

Tests are written using the Check unit testing framework. The tests are located at crypt-test.c, and can be compiled using make test. The check library needs to be installed.

make test
# gcc -Wall test/crypt-test.c -o test/crypt-test -lcheck

test/crypt-test
# Running suite(s): Core
# 100%: Checks: 12, Failures: 0, Errors: 0

About

A simple hashing, encoding, and generation library in C.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages