Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am nileshtrivedi on github.
  • I am nileshtrivedi (https://keybase.io/nileshtrivedi) on keybase.
  • I have a public key ASDS2qJDEl9_uy-LwMNZItAdCCU9ct2v6V57tlhS80cErgo

To claim this, I am signing this object:

@nileshtrivedi
nileshtrivedi / programming.md
Last active December 8, 2020 08:25
Programming: Mostly A Hate Story

Programming: Mostly A Hate Story

I wanted to do digital signatures validation, preferably ed25519, inside PostgreSQL triggers. Here is how it went:

Surely pgcrypto must be supporting it, right? Most Postgres cloud hosting providers already support pgcrypto so this would be perfect. Right?

Well, pgcrypto only supports PGP and that too excludes digital signatures. Let's give PGP a try anyway and see how far can we go.

Installed gpg to generate the keys and the experience is less than pleasant. Sometimes it gets stuck at the passphrase prompt. The keys are too big, but still I can make pgcrypto's pgp_pub_encrypt and pgp_pub_decrypt methods work. Just remeber to convert keys in ASCII to binary and vice-versa using armor()/dearmor(). I hate the big key size in RSA, even though GPG defaults to 2048-bit keys and not the more secure 4096-bit ones. Let's look into ed25519 now.

@nileshtrivedi
nileshtrivedi / sm.rb
Created February 2, 2018 06:22
Multiplication of two state machines
a = {a: [:b, :c], b: [:c]}
def get_edges(sm)
res = []
sm.each do |s, e|
e.each do |d|
res.push([s,d])
end
end
res
@nileshtrivedi
nileshtrivedi / ed25519.sql
Last active February 3, 2023 08:59
ed25519 digital signature methods in PostgreSQL via PL/Python
CREATE EXTENSION IF NOT EXISTS plpythonu;
CREATE FUNCTION py_create_ed25519_keypair ()
RETURNS varchar[]
AS $$
import axolotl_curve25519 as curve
import os
import base64
randm32 = os.urandom(32)
Verifying my Blockstack ID is secured with the address 15e4krfMSXCgWrRpo2FnVRy7DLQ7HuMBQm https://explorer.blockstack.org/address/15e4krfMSXCgWrRpo2FnVRy7DLQ7HuMBQm
@nileshtrivedi
nileshtrivedi / deploy.rb
Last active January 16, 2016 21:01
Deploy a Play framework app with SSHKit
require 'sshkit'
require 'sshkit/dsl'
# Once "bundle install" has been run, this script can be run as "ruby deploy.rb staging" or "ruby deploy.rb prod"
if ARGV.first == "prod"
servers = ['deploy@prod1.example.com', 'deploy@prod2.example.com']
elsif ARGV.first == "staging"
servers = ['deploy@staging.example.com']
else
#!/usr/bin/python
# You need stereovision package to run this. Try "sudo pip install stereovision"
import cv2
from stereovision.blockmatchers import StereoBM, StereoSGBM
from stereovision.calibration import StereoCalibration
from stereovision.stereo_cameras import CalibratedPair
from stereovision.ui_utils import STEREO_BM_FLAG, BMTuner
@nileshtrivedi
nileshtrivedi / hash_builder.rb
Created November 24, 2011 17:49 — forked from brentd/gist:360506
HashBuilder allows you to build a Hash in Ruby similar to Builder with some enhancements
# Allows you to build a Hash in a fashion very similar to Builder. Example:
# Fork of https://gist.github.com/360506 by BrentD with some enhancements
#
# HashBuilder.build! do |h|
# h.name "Nilesh"
# h.skill "Ruby"
# h.skill "Rails" # multiple calls of the same method will collect the values in an array
# h.location "Udaipur, India" do # If a block is given, first argument will be set as value for :name
# h.location do
# h.longitude 24.57
@nileshtrivedi
nileshtrivedi / symbolic.rb
Created October 11, 2011 15:38
Simple symbolic math implementation in Ruby (Work in progress)
# TODO
# if a var has a value, it also needs a name. This should not be necessary. Variable.initialize should accept hashargs
# Expression.to_s should output in infix-notation (like normal humans)
# add more syntactic sugar by manipulating Ruby Numeric classes. Add symbolic operators to Ruby's Numeric
# If symbolic expression contains variables without value then it should return nil (should it, really?)
# Implement additional operators like **
# All symbolic expression should be automatically simplified when created:
# cons(0) * x # => 0
# 2 + x + 1 # => x + 3
# -(x-y) + 2*x # => x + y
@nileshtrivedi
nileshtrivedi / computer.rb
Created October 5, 2011 16:30
A simple virtual machine to experiment with minimal instruction sets
# My attempt to implement a simple virtual machine with a minimal instruction set
# 8-bit computer, fixed-size memory, single cpu, 0-registers, based on von-neumann architecture
# No support for interrupts or I/O although we cheat and provide a "print" instruction :)
# The goal is to find a minimal, but turing-complete, instruction set
# For simplicity, every instruction has two operands, which are located adjacent to it in the memory
# This code is just to experiment with various instruction sets and not to be taken seriously
# Next fun step would be to develop a language/compiler for this virtual machine
class Memory
def initialize(size, program)