Created
July 2, 2021 01:09
-
-
Save 0xabad1dea/7026665beb6de41c17c89beebfb1bb90 to your computer and use it in GitHub Desktop.
SHA1 But I Let Copilot Take The Wheel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SHA1 But I Let Copilot Take The Wheel | |
// this is ENTIRELY written by Github Copilot, all I did was | |
// give it a few prompts and made the absolute minimum changes | |
// to compile, which was rearranging the function order and | |
// adding one variable it had used without declaring. | |
// it does not quite work! have fun fixing it I guess :) | |
// license: uhh... a bot wrote it, I don't think you can | |
// hurt its feelings. | |
// 0xabad1dea July 1 2021 | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
// sha1 ctx struct | |
typedef struct sha1_ctx | |
{ | |
uint32_t buf[5]; | |
uint32_t bits[2]; | |
uint32_t in[16]; | |
} sha1_ctx; | |
uint32_t rol32(uint32_t x, int8_t r) | |
{ | |
return (x << r) | (x >> (32 - r)); | |
} | |
void sha1_init(sha1_ctx* ctx) | |
{ | |
ctx->buf[0] = 0x67452301; | |
ctx->buf[1] = 0xefcdab89; | |
ctx->buf[2] = 0x98badcfe; | |
ctx->buf[3] = 0x10325476; | |
ctx->buf[4] = 0xc3d2e1f0; | |
ctx->bits[0] = 0; | |
ctx->bits[1] = 0; | |
} | |
void sha1_process(sha1_ctx* ctx, uint32_t* data) | |
{ | |
uint32_t a; | |
uint32_t b; | |
uint32_t c; | |
uint32_t d; | |
uint32_t e; | |
uint32_t temp; | |
uint32_t temp2; | |
uint32_t W[80]; | |
uint32_t i; | |
uint32_t j; | |
uint32_t k; | |
uint32_t t; | |
uint32_t *p_buffer; | |
uint32_t *p_buffer_end; | |
uint32_t *p_buffer_start; | |
uint32_t *p_W; | |
uint32_t *p_W_end; | |
p_buffer = ctx->in; | |
p_buffer_end = ctx->in + 16; | |
p_buffer_start = ctx->in; | |
p_W = W; | |
p_W_end = W + 80; | |
while (p_buffer != p_buffer_end) | |
{ | |
*p_W = *p_buffer; | |
p_W++; | |
p_buffer++; | |
} | |
while (p_W != p_W_end) | |
{ | |
*p_W = 0; | |
p_W++; | |
} | |
p_buffer = ctx->in; | |
p_buffer_end = ctx->in + 16; | |
p_W = W; | |
a = ctx->bits[0]; | |
b = ctx->bits[1]; | |
c = ctx->bits[2]; | |
d = ctx->bits[3]; | |
e = ctx->bits[4]; | |
for (i = 0; i < 20; i++) | |
{ | |
temp = (b & c) | ((~b) & d); | |
temp = temp + e + 0x5A827999 + rol32(a, 5); | |
temp = temp + W[i]; | |
e = d; | |
d = c; | |
c = rol32(b, 30); | |
b = a; | |
a = temp; | |
} | |
for (i = 20; i < 40; i++) | |
{ | |
temp = (b & c) | ((~b) & d); | |
temp = temp + e + 0x6ED9EBA1 + rol32(a, 5); | |
temp = temp + W[i]; | |
e = d; | |
d = c; | |
c = rol32(b, 30); | |
b = a; | |
a = temp; | |
} | |
for (i = 40; i < 60; i++) | |
{ | |
temp = (b & c) | ((~b) & d); | |
temp = temp + e + 0x8F1BBCDC + rol32(a, 5); | |
temp = temp + W[i]; | |
e = d; | |
d = c; | |
c = rol32(b, 30); | |
b = a; | |
a = temp; | |
} | |
for (i = 60; i < 80; i++) | |
{ | |
temp = (b & c) | ((~b) & d); | |
temp = temp + e + 0xCA62C1D6 + rol32(a, 5); | |
temp = temp + W[i]; | |
e = d; | |
d = c; | |
c = rol32(b, 30); | |
b = a; | |
a = temp; | |
} | |
ctx->bits[0] += a; | |
ctx->bits[1] += b; | |
ctx->bits[2] += c; | |
ctx->bits[3] += d; | |
ctx->bits[4] += e; | |
} | |
void sha1_final(sha1_ctx* ctx, unsigned char* hash) | |
{ | |
uint32_t i; | |
uint32_t j; | |
uint32_t k; | |
uint32_t t; | |
uint32_t *p_buffer; | |
uint32_t *p_buffer_end; | |
uint32_t *p_buffer_start; | |
uint32_t *p_W; | |
uint32_t *p_W_end; | |
uint32_t *p_W_start; | |
uint32_t size; | |
uint32_t bits; | |
uint32_t *p_hash; | |
uint32_t *p_hash_end; | |
uint32_t *p_hash_start; | |
uint32_t a; | |
uint32_t b; | |
uint32_t c; | |
uint32_t d; | |
uint32_t e; | |
uint32_t temp; | |
uint32_t W[80]; | |
p_buffer = ctx->in; | |
p_buffer_end = ctx->in + 16; | |
p_buffer_start = ctx->in; | |
p_W = W; | |
p_W_end = W + 80; | |
p_W_start = W; | |
p_hash = hash; | |
p_hash_end = hash + 20; | |
p_hash_start = hash; | |
size = ctx->bits[0]; | |
bits = ctx->bits[1]; | |
a = ctx->bits[0]; | |
b = ctx->bits[1]; | |
c = ctx->bits[2]; | |
d = ctx->bits[3]; | |
e = ctx->bits[4]; | |
for (i = 0; i < 16; i++) | |
{ | |
*p_hash = *p_buffer; | |
p_hash++; | |
p_buffer++; | |
} | |
for (i = 16; i < 20; i++) | |
{ | |
*p_hash = 0; | |
p_hash++; | |
} | |
for (i = 0; i < 20; i++) | |
{ | |
*p_hash = rol32(*p_hash, bits); | |
p_hash++; | |
} | |
for (i = 20; i < 40; i++) | |
{ | |
temp = (b & c) | ((~b) & d); | |
temp = temp + e + 0x5A827999 + rol32(a, 5); | |
temp = temp + W[i]; | |
e = d; | |
d = c; | |
c = rol32(b, 30); | |
b = a; | |
a = temp; | |
*p_hash = *p_buffer; | |
p_hash++; | |
p_buffer++; | |
} | |
for (i = 40; i < 60; i++) | |
{ | |
temp = (b & c) | ((~b) & d); | |
temp = temp + e + 0x6ED9EBA1 + rol32(a, 5); | |
temp = temp + W[i]; | |
e = d; | |
d = c; | |
c = rol32(b, 30); | |
b = a; | |
a = temp; | |
*p_hash = *p_buffer; | |
p_hash++; | |
p_buffer++; | |
} | |
for (i = 60; i < 80; i++) | |
{ | |
temp = (b & c) | ((~b) & d); | |
temp = temp + e + 0x8F1BBCDC + rol32(a, 5); | |
temp = temp + W[i]; | |
e = d; | |
d = c; | |
c = rol32(b, 30); | |
b = a; | |
a = temp; | |
*p_hash = *p_buffer; | |
p_hash++; | |
p_buffer++; | |
} | |
for (i = 0; i < 20; i++) | |
{ | |
*p_hash = rol32(*p_hash, bits); | |
p_hash++; | |
} | |
for (i = 20; i < 40; i++) | |
{ | |
temp = (b & c) | ((~b) & d); | |
temp = temp + e + 0x5A827999 + rol32(a, 5); | |
temp = temp + W[i]; | |
e = d; | |
d = c; | |
c = rol32(b, 30); | |
b = a; | |
a = temp; | |
*p_hash = *p_buffer; | |
p_hash++; | |
p_buffer++; | |
} | |
for (i = 40; i < 60; i++) | |
{ | |
temp = (b & c) | ((~b) & d); | |
temp = temp + e + 0x6ED9EBA1 + rol32(a, 5); | |
temp = temp + W[i]; | |
e = d; | |
d = c; | |
c = rol32(b, 30); | |
b = a; | |
a = temp; | |
*p_hash = *p_buffer; | |
p_hash++; | |
p_buffer++; | |
} | |
for (i = 60; i < 80; i++) | |
{ | |
temp = (b & c) | ((~b) & d); | |
temp = temp + e + 0x8F1BBCDC + rol32(a, 5); | |
temp = temp + W[i]; | |
e = d; | |
d = c; | |
c = rol32(b, 30); | |
b = a; | |
a = temp; | |
*p_hash = *p_buffer; | |
p_hash++; | |
p_buffer++; | |
} | |
ctx->bits[0] = size; | |
ctx->bits[1] = bits; | |
ctx->bits[2] = a; | |
ctx->bits[3] = b; | |
ctx->bits[4] = c; | |
ctx->bits[5] = d; | |
ctx->bits[6] = e; | |
} | |
void sha1_update(sha1_ctx* ctx, unsigned char* data, unsigned int len) | |
{ | |
uint32_t i; | |
uint32_t j; | |
uint32_t k; | |
uint32_t w; | |
uint32_t t; | |
uint32_t a; | |
uint32_t b; | |
uint32_t c; | |
uint32_t d; | |
uint32_t e; | |
unsigned char* p; | |
uint32_t buffer[16]; | |
uint32_t size; | |
uint32_t bits; | |
uint32_t *p_buffer; | |
uint32_t *p_buffer_end; | |
uint32_t *p_buffer_start; | |
p = data; | |
size = len; | |
bits = ctx->bits[0]; | |
p_buffer = ctx->in; | |
p_buffer_end = ctx->in + 16; | |
p_buffer_start = ctx->in; | |
while (size > 0) | |
{ | |
if (p_buffer == p_buffer_end) | |
{ | |
sha1_process(ctx, p_buffer_start); | |
p_buffer = ctx->in; | |
p_buffer_start = p_buffer_end; | |
p_buffer_end = p_buffer_end + 16; | |
} | |
*p_buffer = *p; | |
p++; | |
p_buffer++; | |
size--; | |
} | |
ctx->bits[0] = bits; | |
ctx->bits[1] = 0; | |
} | |
void sha1(unsigned char* buffer, int buffer_size, unsigned char* hash) | |
{ | |
sha1_ctx ctx; | |
sha1_init(&ctx); | |
sha1_update(&ctx, buffer, buffer_size); | |
sha1_final(&ctx, hash); | |
} | |
void sha1_of_string(char* string, unsigned char* hash) | |
{ | |
unsigned char hash_buffer[20]; | |
sha1(string, strlen(string), hash_buffer); | |
for (int i = 0; i < 20; i++) | |
{ | |
hash[i] = hash_buffer[i]; | |
} | |
} | |
// calculates the sha1 hash of argv[1] | |
int main(int argc, char** argv) | |
{ | |
if (argc != 2) | |
{ | |
printf("usage: %s <string>\n", argv[0]); | |
return 1; | |
} | |
sha1_of_string(argv[1], (unsigned char*)argv[2]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment