Skip to content

Instantly share code, notes, and snippets.

View starbeast's full-sized avatar

Artsiom Kuts starbeast

View GitHub Profile
@starbeast
starbeast / gist:9cfbc9f499bff84a57db815a2dfa81d2
Created November 24, 2019 13:55
NaCl secret box (RbNaCl BE + TweetNaCl.js FE)
Ruby - https://github.com/RubyCrypto/rbnacl
JavaScript - https://github.com/dchest/tweetnacl-js
## FE encrypt -> BE decrypt
# FE
const { publicKey, secretKey } = nacl.box.keyPair();
// every time you encrypt change the nonce
const nonce = nacl.randomBytes(nacl.secretbox.nonceLength);
const encodedNonce = nacl.util.encodeBase64(nonce);
const encodedPublic = nacl.util.encodeBase64(publicKey);
@starbeast
starbeast / example.ts
Last active June 5, 2017 20:34
Mixins + DI
import {
Injectable,
Inject,
OpaqueToken
} from '@angular/core';
let resolveParameters = function (target: any, params: any[]) {
if(params) {
params.forEach((param, index) => {
// this code is needed to make @Inject work
@starbeast
starbeast / example.ts
Last active June 5, 2017 18:00
MixinInjectable
let resolveParameters = function (target: any, params: any[]) {
if(params) {
params.forEach((param, index) => {
// this code is needed to make @Inject work
if (param && param.annotation && param.annotation instanceof Inject) {
param(target, void 0, index);
}
});
}
return Reflect.getMetadata('parameters', target);
@starbeast
starbeast / example.ts
Last active June 5, 2017 17:57
Decorated mixin
@MixinInjectable(Dependency, AnotherDependency, Inject(YetAnotherDependency))
class Mixin extends Base {
dependency: Dependency;
anotherDependency: AnotherDependency;
yetAnotherDependency: IYetAnotherDependency;
constructor(...args: any[]) {
super(...args.slice(3));
this.dependency = args[0];
@starbeast
starbeast / example.ts
Created June 4, 2017 14:22
Mixin with constraints
interface IPoint {
x: number;
y: number;
}
const WithLocation = <T extends Constructor<IPoint>>(Base: T) =>
class extends Base {
getLocation(): [number, number] {
return [this.x, this.y];
}
@starbeast
starbeast / example.ts
Last active April 16, 2019 07:56
TypeScript mixins example
class Point {
constructor(public x: number, public y: number) {}
}
class Person {
constructor(public name: string) {}
}
type Constructor<T> = new(...args: any[]) => T;