コード例 #1
0
ファイル: sha3module.c プロジェクト: DinoV/cpython
static PyObject *
py_sha3_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
    SHA3object *self = NULL;
    Py_buffer buf = {NULL, NULL};
    HashReturn res;
    PyObject *data = NULL;

    if (!_PyArg_NoKeywords(_PyType_Name(type), kwargs)) {
        return NULL;
    }
    if (!PyArg_UnpackTuple(args, _PyType_Name(type), 0, 1, &data)) {
        return NULL;
    }

    self = newSHA3object(type);
    if (self == NULL) {
        goto error;
    }

    if (type == &SHA3_224type) {
        res = Keccak_HashInitialize_SHA3_224(&self->hash_state);
    } else if (type == &SHA3_256type) {
        res = Keccak_HashInitialize_SHA3_256(&self->hash_state);
    } else if (type == &SHA3_384type) {
        res = Keccak_HashInitialize_SHA3_384(&self->hash_state);
    } else if (type == &SHA3_512type) {
        res = Keccak_HashInitialize_SHA3_512(&self->hash_state);
#ifdef PY_WITH_KECCAK
    } else if (type == &Keccak_224type) {
        res = Keccak_HashInitialize(&self->hash_state, 1152, 448, 224, 0x01);
    } else if (type == &Keccak_256type) {
        res = Keccak_HashInitialize(&self->hash_state, 1088, 512, 256, 0x01);
    } else if (type == &Keccak_384type) {
        res = Keccak_HashInitialize(&self->hash_state, 832, 768, 384, 0x01);
    } else if (type == &Keccak_512type) {
        res = Keccak_HashInitialize(&self->hash_state, 576, 1024, 512, 0x01);
#endif
    } else if (type == &SHAKE128type) {
        res = Keccak_HashInitialize_SHAKE128(&self->hash_state);
    } else if (type == &SHAKE256type) {
        res = Keccak_HashInitialize_SHAKE256(&self->hash_state);
    } else {
        PyErr_BadInternalCall();
        goto error;
    }

    if (data) {
        GET_BUFFER_VIEW_OR_ERROR(data, &buf, goto error);
        if (buf.len >= HASHLIB_GIL_MINSIZE) {
            /* invariant: New objects can't be accessed by other code yet,
             * thus it's safe to release the GIL without locking the object.
             */
            Py_BEGIN_ALLOW_THREADS
            res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
            Py_END_ALLOW_THREADS
        }
        else {
コード例 #2
0
ファイル: sha3module.c プロジェクト: 3lnc/cpython
static PyObject *
py_sha3_new_impl(PyTypeObject *type, PyObject *data)
/*[clinic end generated code: output=8d5c34279e69bf09 input=d7c582b950a858b6]*/
{
    SHA3object *self = NULL;
    Py_buffer buf = {NULL, NULL};
    HashReturn res;

    self = newSHA3object(type);
    if (self == NULL) {
        goto error;
    }

    if (type == &SHA3_224type) {
        res = Keccak_HashInitialize_SHA3_224(&self->hash_state);
    } else if (type == &SHA3_256type) {
        res = Keccak_HashInitialize_SHA3_256(&self->hash_state);
    } else if (type == &SHA3_384type) {
        res = Keccak_HashInitialize_SHA3_384(&self->hash_state);
    } else if (type == &SHA3_512type) {
        res = Keccak_HashInitialize_SHA3_512(&self->hash_state);
#ifdef PY_WITH_KECCAK
    } else if (type == &Keccak_224type) {
        res = Keccak_HashInitialize(&self->hash_state, 1152, 448, 224, 0x01);
    } else if (type == &Keccak_256type) {
        res = Keccak_HashInitialize(&self->hash_state, 1088, 512, 256, 0x01);
    } else if (type == &Keccak_384type) {
        res = Keccak_HashInitialize(&self->hash_state, 832, 768, 384, 0x01);
    } else if (type == &Keccak_512type) {
        res = Keccak_HashInitialize(&self->hash_state, 576, 1024, 512, 0x01);
#endif
    } else if (type == &SHAKE128type) {
        res = Keccak_HashInitialize_SHAKE128(&self->hash_state);
    } else if (type == &SHAKE256type) {
        res = Keccak_HashInitialize_SHAKE256(&self->hash_state);
    } else {
        PyErr_BadInternalCall();
        goto error;
    }

    if (data) {
        GET_BUFFER_VIEW_OR_ERROR(data, &buf, goto error);
#ifdef WITH_THREAD
        if (buf.len >= HASHLIB_GIL_MINSIZE) {
            /* invariant: New objects can't be accessed by other code yet,
             * thus it's safe to release the GIL without locking the object.
             */
            Py_BEGIN_ALLOW_THREADS
            res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
            Py_END_ALLOW_THREADS
        }
        else {
コード例 #3
0
void calculateAddress(const uint8_t* data, size_t size, char* nemAddress)
{
	unsigned char sha3result[32];

	Keccak_HashInstance _hctx, *hctx = &_hctx;
	Keccak_HashInitialize_SHA3_256(hctx);
	Keccak_HashUpdate(hctx, data, size * 8);
	Keccak_HashSqueeze(hctx, sha3result, 256);

	unsigned char r160result[25];
	computeRIPEMD160(sha3result, 32, r160result + 1);
	r160result[0] = 0x68;

	Keccak_HashInitialize_SHA3_256(hctx);
	Keccak_HashUpdate(hctx, r160result, 21 * 8);
	Keccak_HashSqueeze(hctx, sha3result, 256);

	*(uint32_t*)(r160result + 21) = *(uint32_t*)(sha3result);

	base32_encode(r160result, 25, (unsigned char*)nemAddress);
	nemAddress[40] = 0;
}
コード例 #4
0
ファイル: sha3.c プロジェクト: kryptohash/kryptohash
unsigned char *SHA3_256(const unsigned char *dataIn, size_t nBytesIn, unsigned char *md)
{
    Keccak_HashInstance h;
    static BitSequence  m[SHA3_256_DL];

    if (md == NULL) {
        md = m;
    }
    Keccak_HashInitialize_SHA3_256(&h);
    Keccak_HashUpdate(&h, dataIn, (DataLength)nBytesIn * 8);
    Keccak_HashFinal(&h, md);

    return(md);
}
コード例 #5
0
ファイル: sha3.c プロジェクト: bjornedstrom/python-sha3
static PyObject *
SHA3_init(SHAobject *self, PyObject *args)
{
    int hashbitlen;
    int outputlen;

    if (!PyArg_ParseTuple(args, "ii", &hashbitlen, &outputlen))
        return NULL;

    self->hashbitlen = hashbitlen;
    self->outputlen = outputlen;

    switch (hashbitlen) {
    case 224:
        Keccak_HashInitialize_SHA3_224(&self->state);
        break;
    case 256:
        Keccak_HashInitialize_SHA3_256(&self->state);
        break;
    case 384:
        Keccak_HashInitialize_SHA3_384(&self->state);
        break;
    case 512:
        Keccak_HashInitialize_SHA3_512(&self->state);
        break;

    // HACK: This is pretty ugly :-)
    case 10128:
        Keccak_HashInitialize_SHAKE128(&self->state);
        break;
    case 10256:
        Keccak_HashInitialize_SHAKE256(&self->state);
        break;
    };

    Py_INCREF(Py_None);
    return Py_None;
}
コード例 #6
0
ファイル: sha3.c プロジェクト: Juju17i/sha3-cli
int main(int argc, char* argv[]) {

  if (argc < 3) {
    print_usage();
  }

  int hash_size = atoi(argv[1]);
  Keccak_HashInstance hash;

  HashReturn initResult = FAIL;
  if (hash_size == 224) {

    initResult = Keccak_HashInitialize_SHA3_224(&hash);

  } else if (hash_size == 256) {

    initResult = Keccak_HashInitialize_SHA3_256(&hash);

  } else if (hash_size == 384) {

    initResult = Keccak_HashInitialize_SHA3_384(&hash);

  } else if (hash_size == 512) {

    initResult = Keccak_HashInitialize_SHA3_512(&hash);

  } else {

    printf("Error : wrong size argument\nAvailable sizes : 224, 256, 384, 512\n");
    exit(0);

  }

  if (initResult != SUCCESS) {
    printf("error when itializing SHA-3\n");
    return -1;
  }

  if (Keccak_HashUpdate(&hash, (unsigned char*)argv[STRING_POSITION], strlen(argv[STRING_POSITION]) * 8) != SUCCESS) {
    printf("error when updating SHA-3\n");
    return -1;
  }

  int bytes_hash_size = hash_size / 8;
  unsigned char* output = malloc(bytes_hash_size);
  if (output == NULL) {
    printf("memory allocation error\n");
    return -1;
  }

  if (Keccak_HashFinal(&hash, output) != SUCCESS) {
    printf("error when extracting the result hash\n");
    return -1;
  }

  int i;
  for (i = 0; i < bytes_hash_size; i++) {
    printf("%02x", (unsigned int)output[i]);
  }

  putchar('\n');

  return 0;
}