Skip to content

Instantly share code, notes, and snippets.

@bellbind
bellbind / ecc.py
Created December 1, 2011 08:08
[python]basics of elliptic curve cryptography
View ecc.py
# Basics of Elliptic Curve Cryptography implementation on Python
import collections
def inv(n, q):
"""div on PN modulo a/b mod q as a * inv(b, q) mod q
>>> assert n * inv(n, q) % q == 1
"""
for i in range(q):
if (n * i) % q == 1:
@bellbind
bellbind / index.html
Last active January 13, 2023 16:12
[chrome][android] BarcodeDetector example
View index.html
<!doctype html>
<html>
<head>
<script type="module">
// WICG Shape Detection API
// - https://wicg.github.io/shape-detection-api/
try {
const start = document.getElementById("start");
const video = document.getElementById("video");
const result = document.getElementById("result");
@bellbind
bellbind / DisplayMode.swift
Last active January 11, 2023 16:55
[macos][c]commandline for display and switch display mode
View DisplayMode.swift
#!/usr/bin/env swift
// swiftc displaymode1.swift
import Foundation
import CoreGraphics
let display = CGMainDisplayID()
guard let dmodes = CGDisplayCopyAllDisplayModes(display, nil) as? [CGDisplayMode] else {exit(1)}
let argv = CommandLine.arguments
if argv.count == 2 {
@bellbind
bellbind / capture.c
Last active December 15, 2022 02:18
[linux][v4l2][libjpeg][c99]example for capturing from UVC webcam (on beaglebone black)
View capture.c
/*
* capturing from UVC cam
* requires: libjpeg-dev
* build: gcc -std=c99 capture.c -ljpeg -o capture
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@bellbind
bellbind / pem.js
Last active December 13, 2022 02:55
[nodejs]Example of RSA usages with node-forge
View pem.js
// RSA with node-forge
"use strict";
// npm install node-forge
const forge = require("node-forge");
new Promise((f, r) => forge.pki.rsa.generateKeyPair(
2048, (err, pair) => err ? r(err) : f(pair)))
.then(keypair => {
const priv = keypair.privateKey;
@bellbind
bellbind / closable-stream.js
Last active December 11, 2022 14:46
[js-ipfs/libp2p] dial to read events from infinite sending from handler, then close read
View closable-stream.js
// wrapping libp2p stream (mplex/stream)
// - stream.source: AsyncIterable<Uint8Array>
// - stream.sink: (Iterable<Uint8Array> | AsyncIterable<Uint8Array>) => Promise<undefined>
// - stream.close, stream.closeRead, stream.closeWrite, stream.abort, stream.reset
const newQueue = () => {
const [gets, polls] = [[], []];
const next = () => new Promise(
get => polls.length > 0 ? polls.shift()(get) : gets.push(get));
const poll = () => new Promise(
poll => gets.length > 0 ? poll(gets.shift()) : polls.push(poll));
@bellbind
bellbind / cache-method.js
Last active November 19, 2022 14:46
[Web Cache API][browser][deno] Web Cache API basic examples
View cache-method.js
await caches.delete("tmp");
const cache = await caches.open("tmp");
const req1 = new Request("http://example.com/");
const req2 = new Request("http://example.com/", {method: "head"});
const req3 = new Request("http://example.com/", {method: "post"});
const res1 = new Response("hello en-US", {headers: {"Content-Type": "text/plain;charset=utf-8"}});
const res2 = new Response("", {headers: {}});
await cache.put(req1, res1); // cache "http://example.com/ with res1
@bellbind
bellbind / example-relay-http2p.mjs
Created November 17, 2022 13:51
[ipfs][nodejs] HTTP like messaging for PeerID via libp2p in IPFS node
View example-relay-http2p.mjs
#!/usr/bin/env node
import * as fs from "node:fs";
// IPFS
import * as IPFS from "ipfs-core";
// WebRTCStar
import wrtc from "@koush/wrtc";
import {sigServer} from "@libp2p/webrtc-star-signalling-server";
import {webRTCStar} from "@libp2p/webrtc-star";
// pubsub peerDiscovery
//import {floodsub} from "@libp2p/floodsub";
@bellbind
bellbind / index.html
Last active November 12, 2022 14:30
[javascript][browser] Simple Turing Pattern simulator
View index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<script src="script.js" defer="defer"></script>
</head>
<body>
</body>
</html>
@bellbind
bellbind / tetris-svg.html
Created November 20, 2012 09:22
[javascript][svg]tetris model with svg ui (arrow key control)
View tetris-svg.html
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1" />
<script src="tetris.js"></script>
<script src="tetris-svg.js"></script>
</head>
<body>
<div><svg id="stage"></svg></div>