<commit_before># BSCrypt (Basic Crypt)

This is a **basic** crypto library in C.

At the moment, we have:

* SHA-1.
* SHA-2.
* SipHash2-4 (unstreamed).
* Unix crypto-level pseudo-random.
* Base64 encode/decode (not crypto but useful).
* Hex encode/decode (not crypto but useful).
* XOR masking (not crypto but useful)... I'll write this again, **not** crypto - don't use XOR for encrypting stuff!

Future plans (optional, feel free to open a PR with any of these features):

* SHA-3.
* AES GCM / CCM modes encryption/decryption.
* RSA / DH / DHEC.
* TLS 1.2 / 1.3 (I'll probably never get this far, but it would be fun to have a library with a simple API for this).


The library is modular, mostly. Meaning you can copy the [`bscrypt-common.h`](./src/bscrypt/bscrypt-common.h) and any specific feature you want (i.e. take just the SHA-1 by copying the `sha1.h` and `sha1.c` files).

## Sample use

The code is heavily documented, but it does assume you know what you're doing (hardly any overflow / NULL pointer protection).

Some things, like Base64/Hex encoding assume you allocated enough space (the functions are unsafe) and overlapping memory will cause undefined behavior (although decoding will mostly work when performed in place).

The purpose of this lower level API was to allow higher level APIs to be written, so use the provided API with care.

Here's a simple SHA-1 computation:

```c
#include "bscrypt.h"

int main(int argv, const char * argv[]) {
  char * result;
  sha1_s sha1 = bscrypt_sha1_init();
  bscrypt_sha1_write(&sha1, "The quick brown fox jumps over the lazy dog ", 43);
  result = bscrypt_sha1_result(&sha1);
  assert(result == sha1.digest.str); /* locally stored on the stack */
  /* another option is to use a single call with the allocated sha1 variable */
  result = bscrypt_sha1(&sha1,
                        "The quick brown fox jumps over the lazy dog ",
                        43);
  /**/
}
```

## Requirements

The C code is mostly portable (at least, I hope it is) and should work on most (or all) systems.

Some features (i.e. the Pseudo-Random generator) will only work properly on Unix systems... although I did write a fallback alternative for the Pseudo-Random generator, it isn't idle.

I didn't check the code on any system except my Mac OS X and Linux, so you might have to rewrite some include directives (I didn't bother checking the build on anything other then Linux and OS X).

The code wasn't really optimized that much, but what optimizations I did make, I oriented towards 64bit architectures. The SHA-1 and SHA-2 seemed fairly fast on my system (give or take on par with OpenSSL) but slightly slower on the Linux machine (where OpenSSL was noticeably faster)... but it might be because my older hardware wasn't running hardware optimizations in use by OpenSSL.

## Randomness

The pseudo random generator follows the Unix/Linux recommendation of opening the `/dev/urandom` pseudo random stream and reading from it...<commit_msg>Fixed and Update README<commit_after># BSCrypt (Basic Crypt)

This is a **basic** crypto library in C.

At the moment, we have:

* SHA-1.
* SHA-2.
* SipHash2-4 (unstreamed).
* Unix crypto-level pseudo-random.
* Base64 encode/decode (not crypto but useful).
* Hex encode/decode (not crypto but useful).
* XOR masking (not crypto but useful)... I'll write this again, **not** crypto - don't use XOR for encrypting stuff!

Future plans (optional, feel free to open a PR with any of these features):

* SHA-3.
* AES GCM / CCM modes encryption/decryption.
* RSA / DH / DHEC.
* TLS 1.2 / 1.3 (I'll probably never get this far, but it would be fun to have a library with a simple API for this).


The library is modular, mostly. Meaning you can copy the [`bscrypt-common.h`](./src/bscrypt/bscrypt-common.h) and any specific feature you want (i.e. take just the SHA-1 by copying the `sha1.h` and `sha1.c` files).

## Sample use

The code is heavily documented, but it does assume you know what you're doing (hardly any overflow / NULL pointer protection).

Some things, like Base64/Hex encoding assume you allocated enough space (the functions are unsafe) and overlapping memory will cause undefined behavior (although decoding will mostly work when performed in place).

The purpose of this lower level API was to allow higher level APIs to be written, so use the provided API with care.

Here's a simple SHA-1 computation:

```c
#include "bscrypt.h"
#include <assert.h>

int main(int argv, const char * argv[]) {
  char * result;
  sha1_s sha1 = bscrypt_sha1_init();
  bscrypt_sha1_write(&sha1, "The quick brown fox jumps over the lazy dog ", 43);
  result = bscrypt_sha1_result(&sha1);
  assert(result == (cahr *)sha1.digest.str); /* locally stored on the stack */
  /* another option is to use a single call with the allocated sha1 variable */
  result = bscrypt_sha1(&sha1,
                        "The quick brown fox jumps over the lazy dog ",
                        43);
  /**/
}
```

## Requirements

The C code is mostly portable (at least, I hope it is) and should work on most (or all) systems.

Some features (i.e. the Pseudo-Random generator) will only work properly on Unix systems... although I did write a fallback alternative for the Pseudo-Random generator, it isn't idle.

I didn't check the code on any system except my Mac OS X and Linux, so you might have to rewrite some include directives (I didn't bother checking the build on anything other then Linux and OS X).

The code wasn't really optimized that much, but what optimizations I did make, I oriented towards 64bit architectures. The SHA-1 and SHA-2 seemed fairly fast on my system (give or take on par with OpenSSL) but slightly slower on the Linux machine (where OpenSSL was noticeably faster)... but it might be because my older hardware wasn't running hardware optimizations in use by OpenSSL.

## Randomness

The pseudo random generator follows the Unix/Linux recommendation of opening the `/dev/urandom` pseudo random stream and reading from it...
{'fraction_non_alphanumeric': 0.0813102760226139, 'fraction_numerical': 0.011972065181243765, 'mean_word_length': 3.6627906976744184, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3597905', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1650, 'n_words': 881}