sha0 hashing c package for converting passwords in softether config
This commit is contained in:
parent
5fff2a6a16
commit
8a1f63b557
4 changed files with 200 additions and 1 deletions
|
|
@ -10,6 +10,9 @@
|
|||
pname = "softether-5";
|
||||
version = "5.02.5187";
|
||||
in {
|
||||
packages.${system}.default = pkgs.callPackage ./package.nix { inherit pname version; };
|
||||
packages.${system} = {
|
||||
default = pkgs.callPackage ./package.nix { inherit pname version; };
|
||||
sha0 = pkgs.callPackage ./sha0 {};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
|
|||
20
sha0/default.nix
Normal file
20
sha0/default.nix
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{ pkgs, stdenv, ... }:
|
||||
stdenv.mkDerivation {
|
||||
pname = "sha0";
|
||||
version = "0.1.0";
|
||||
|
||||
src = ./.;
|
||||
|
||||
buildPhase = ''
|
||||
cc -O2 -o sha0 sha0.c main.c
|
||||
'';
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p $out/bin
|
||||
cp sha0 $out/bin/
|
||||
'';
|
||||
|
||||
meta = with pkgs.lib; {
|
||||
description = "SHA-0 hashing CLI tool";
|
||||
};
|
||||
}
|
||||
24
sha0/main.c
Normal file
24
sha0/main.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// Declare the function from sha0.c
|
||||
void Internal_Sha0(unsigned char *dest, const unsigned char *src, const unsigned int size);
|
||||
|
||||
#define SHA0_DIGEST_SIZE 20
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Usage: %s <string>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned char digest[SHA0_DIGEST_SIZE];
|
||||
Internal_Sha0(digest, (const unsigned char *)argv[1], strlen(argv[1]));
|
||||
|
||||
for (int i = 0; i < SHA0_DIGEST_SIZE; i++) {
|
||||
printf("%02x", digest[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
152
sha0/sha0.c
Normal file
152
sha0/sha0.c
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
// Copied from softether's Encrypt.c
|
||||
/////////////////////////
|
||||
// SHA0 implementation //
|
||||
/////////////////////////
|
||||
|
||||
// Source codes from:
|
||||
// https://android.googlesource.com/platform/system/core/+/81df1cc77722000f8d0025c1ab00ced123aa573c/libmincrypt/sha.c
|
||||
// https://android.googlesource.com/platform/system/core/+/81df1cc77722000f8d0025c1ab00ced123aa573c/include/mincrypt/hash-internal.h
|
||||
// https://android.googlesource.com/platform/system/core/+/81df1cc77722000f8d0025c1ab00ced123aa573c/include/mincrypt/sha.h
|
||||
|
||||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its contributors may
|
||||
* be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
* EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef unsigned long long UINT64;
|
||||
typedef unsigned char UCHAR;
|
||||
typedef unsigned int UINT;
|
||||
|
||||
#define rol(bits, value) (((value) << (bits)) | ((value) >> (32 - (bits))))
|
||||
|
||||
typedef struct MY_SHA0_CTX {
|
||||
// const HASH_VTAB * f;
|
||||
UINT64 count;
|
||||
UCHAR buf[64];
|
||||
UINT state[8]; // upto SHA2
|
||||
} MY_SHA0_CTX;
|
||||
|
||||
#define MY_SHA0_DIGEST_SIZE 20
|
||||
|
||||
static void MY_SHA0_Transform(MY_SHA0_CTX* ctx) {
|
||||
UINT W[80];
|
||||
UINT A, B, C, D, E;
|
||||
UCHAR* p = ctx->buf;
|
||||
int t;
|
||||
for(t = 0; t < 16; ++t) {
|
||||
UINT tmp = *p++ << 24;
|
||||
tmp |= *p++ << 16;
|
||||
tmp |= *p++ << 8;
|
||||
tmp |= *p++;
|
||||
W[t] = tmp;
|
||||
}
|
||||
for(; t < 80; t++) {
|
||||
//W[t] = rol(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
|
||||
W[t] = (1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
|
||||
}
|
||||
A = ctx->state[0];
|
||||
B = ctx->state[1];
|
||||
C = ctx->state[2];
|
||||
D = ctx->state[3];
|
||||
E = ctx->state[4];
|
||||
for(t = 0; t < 80; t++) {
|
||||
UINT tmp = rol(5,A) + E + W[t];
|
||||
if (t < 20)
|
||||
tmp += (D^(B&(C^D))) + 0x5A827999;
|
||||
else if ( t < 40)
|
||||
tmp += (B^C^D) + 0x6ED9EBA1;
|
||||
else if ( t < 60)
|
||||
tmp += ((B&C)|(D&(B|C))) + 0x8F1BBCDC;
|
||||
else
|
||||
tmp += (B^C^D) + 0xCA62C1D6;
|
||||
E = D;
|
||||
D = C;
|
||||
C = rol(30,B);
|
||||
B = A;
|
||||
A = tmp;
|
||||
}
|
||||
ctx->state[0] += A;
|
||||
ctx->state[1] += B;
|
||||
ctx->state[2] += C;
|
||||
ctx->state[3] += D;
|
||||
ctx->state[4] += E;
|
||||
}
|
||||
void MY_SHA0_init(MY_SHA0_CTX* ctx) {
|
||||
//ctx->f = &SHA_VTAB;
|
||||
ctx->state[0] = 0x67452301;
|
||||
ctx->state[1] = 0xEFCDAB89;
|
||||
ctx->state[2] = 0x98BADCFE;
|
||||
ctx->state[3] = 0x10325476;
|
||||
ctx->state[4] = 0xC3D2E1F0;
|
||||
ctx->count = 0;
|
||||
}
|
||||
void MY_SHA0_update(MY_SHA0_CTX* ctx, const void* data, int len) {
|
||||
int i = (int) (ctx->count & 63);
|
||||
const UCHAR* p = (const UCHAR*)data;
|
||||
ctx->count += len;
|
||||
while (len--) {
|
||||
ctx->buf[i++] = *p++;
|
||||
if (i == 64) {
|
||||
MY_SHA0_Transform(ctx);
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
const UCHAR* MY_SHA0_final(MY_SHA0_CTX* ctx) {
|
||||
UCHAR *p = ctx->buf;
|
||||
UINT64 cnt = ctx->count * 8;
|
||||
int i;
|
||||
MY_SHA0_update(ctx, (UCHAR*)"\x80", 1);
|
||||
while ((ctx->count & 63) != 56) {
|
||||
MY_SHA0_update(ctx, (UCHAR*)"\0", 1);
|
||||
}
|
||||
for (i = 0; i < 8; ++i) {
|
||||
UCHAR tmp = (UCHAR) (cnt >> ((7 - i) * 8));
|
||||
MY_SHA0_update(ctx, &tmp, 1);
|
||||
}
|
||||
for (i = 0; i < 5; i++) {
|
||||
UINT tmp = ctx->state[i];
|
||||
*p++ = tmp >> 24;
|
||||
*p++ = tmp >> 16;
|
||||
*p++ = tmp >> 8;
|
||||
*p++ = tmp >> 0;
|
||||
}
|
||||
return ctx->buf;
|
||||
}
|
||||
/* Convenience function */
|
||||
const UCHAR* MY_SHA0_hash(const void* data, int len, UCHAR* digest) {
|
||||
MY_SHA0_CTX ctx;
|
||||
MY_SHA0_init(&ctx);
|
||||
MY_SHA0_update(&ctx, data, len);
|
||||
memcpy(digest, MY_SHA0_final(&ctx), MY_SHA0_DIGEST_SIZE);
|
||||
return digest;
|
||||
}
|
||||
|
||||
void Internal_Sha0(unsigned char *dest, const unsigned char *src, const UINT size)
|
||||
{
|
||||
MY_SHA0_hash(src, (int)size, dest);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue