Skip to content

Instantly share code, notes, and snippets.

View nakov's full-sized avatar
🎯
Focused on the global SoftUni expansion: https://softuni.org

Svetlin Nakov nakov

🎯
Focused on the global SoftUni expansion: https://softuni.org
View GitHub Profile
@nakov
nakov / Cert.sol
Created February 16, 2018 12:32
Document Registry - Infura API
pragma solidity ^0.4.18;
contract Cert {
mapping (string => bool) private certificateHashes;
address contractOwner = msg.sender;
function add(string hash) public {
require (msg.sender == contractOwner);
certificateHashes[hash] = true;
}
@nakov
nakov / rsk-node.conf
Created February 20, 2018 10:21
Start RSK Node in Windows - Example
blockchain.config.name = "regtest"
peer {
discovery = {
# if peer discovery is off
# the peer window will show
# only what retrieved by active
# peer [true/false]
@nakov
nakov / transactionSigner.js
Created March 7, 2018 10:32
Sign a transaction in JS
const CryptoJS = require("crypto-js");
const EC = require('elliptic').ec;
const secp256k1 = new EC('secp256k1');
function signData(data, privKey) {
let keyPair = secp256k1.keyFromPrivate(privKey);
let signature = keyPair.sign(data);
return [signature.r.toString(16), signature.s.toString(16)];
}
@nakov
nakov / modsqrt.py
Created March 7, 2018 13:00
mod_sqrt - Python 3 implementation
def modular_sqrt(a, p):
def legendre_symbol(a, p):
""" Compute the Legendre symbol a|p using
Euler's criterion. p is a prime, a is
relatively prime to p (if p divides
a, then a|p = 0)
Returns 1 if a has a square root modulo
p, -1 otherwise.
@nakov
nakov / compress-ec-key.py
Created March 7, 2018 13:08
Compress / decompress elliptic curve public key in Pyhton
from pycoin.ecdsa import CurveFp, Point
from nummaster.basic import sqrtmod
def compress_key_pair(key_pair):
return (key_pair.x(), key_pair.y() % 2)
def uncompress_key(curve, compressed_key):
x, is_odd = compressed_key
p, a, b = curve.p(), curve.a(), curve.b()
y = sqrtmod(pow(x, 3, p) + a * x + b, p)
@nakov
nakov / CryptoUtils.js
Last active November 16, 2018 22:26
JS Crypto Utils - secp256k1 EC cryptography and blockchain functions
const CryptoJS = require("crypto-js");
const EC = require('elliptic').ec;
const secp256k1 = new EC('secp256k1');
function publicKeyToAddress(pubKey) {
let address = CryptoJS.RIPEMD160(pubKey).toString();
return address;
}
function privateKeyToPublicKey(privKey) {
@nakov
nakov / secp256k1-example.js
Last active April 9, 2024 16:32
ECDSA in JavaScript: secp256k1-based sign / verify / recoverPubKey
let elliptic = require('elliptic');
let sha3 = require('js-sha3');
let ec = new elliptic.ec('secp256k1');
// let keyPair = ec.genKeyPair();
let keyPair = ec.keyFromPrivate("97ddae0f3a25b92268175400149d65d6887b9cefaf28ea2c078e05cdc15a3c0a");
let privKey = keyPair.getPrivate("hex");
let pubKey = keyPair.getPublic();
console.log(`Private key: ${privKey}`);
console.log("Public key :", pubKey.encode("hex").substr(2));
@nakov
nakov / ECDSA-sign-verify.py
Created April 5, 2018 10:31
ECDSA with sec256k1 Example in Python - Generate Keys, Sign, Verify
import eth_keys, eth_utils, binascii, os
# privKey = eth_keys.keys.PrivateKey(os.urandom(32))
privKey = eth_keys.keys.PrivateKey(binascii.unhexlify(
'97ddae0f3a25b92268175400149d65d6887b9cefaf28ea2c078e05cdc15a3c0a'))
pubKey = privKey.public_key
pubKeyCompressed = '0' + str(2 + int(pubKey) % 2) + str(pubKey)[2:66]
address = pubKey.to_checksum_address()
print('Private key (64 hex digits):', privKey)
print('Public key (plain, 128 hex digits):', pubKey)
@nakov
nakov / ECDSA-secp256k1-example.java
Last active July 21, 2023 09:37
ECDSA with secp256k1 in Java: generate ECC keys, sign, verify
import org.bouncycastle.util.encoders.Hex;
import org.web3j.crypto.*;
import java.math.BigInteger;
public class ECCExample {
public static String compressPubKey(BigInteger pubKey) {
String pubKeyYPrefix = pubKey.testBit(0) ? "03" : "02";
String pubKeyHex = pubKey.toString(16);
String pubKeyX = pubKeyHex.substring(0, 64);
@nakov
nakov / ECDSA-secp256k1-example.cs
Last active October 26, 2022 23:18
ECDSA with secp256k1 in C# - Generate Keys, Sign, Verify
using System;
using System.Text;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.Signer;
using Nethereum.Util;
using Nethereum.Signer.Crypto;
class ECDSASecp256k1Example
{
static void Main()