Skip to content

Instantly share code, notes, and snippets.

View sle-c's full-sized avatar
🏠
Working from home

Si Le sle-c

🏠
Working from home
View GitHub Profile
@sle-c
sle-c / encrypt.go
Created April 1, 2019 02:26
Encrypt text using AES256 GCM in golang
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"os"
)
// Encrypt will encrypt a raw string to
// an encrypted value
@sle-c
sle-c / decrypt.go
Last active April 1, 2019 02:26
Decrypt text using AES256 GCM in golang
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"os"
)
// Decrypt will return the original value of the encrypted string
func Decrypt(encryptedKey []byte) ([]byte, error) {
@sle-c
sle-c / create_random_key.go
Last active April 1, 2019 00:48
Create random key string
import (
"crypto/rand"
)
func NewRandomKey() []byte {
key := make([]byte, 32)
if _, err := rand.Read(key); err != nil {
// really, what are you gonna do if randomness failed?
panic(err)
}
@sle-c
sle-c / create_apps_table.sql
Last active April 1, 2019 02:16
JWT Auth with Golang
CREATE TABLE apps (
id SERIAL PRIMARY KEY,
name character varying,
public_key character varying,
encrypted_secret_key character varying,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
-- enhance query performance and enforce uniqueness
CREATE UNIQUE INDEX apps_public_key ON apps(public_key text_ops);
@sle-c
sle-c / activity_subscription.js
Last active June 10, 2020 00:13
Do something if initial load
function handleActivitySubscription(snapshot, counter) {
const initialLoad = counter === 1;
snapshot.docChanges().forEach(function(change) {
if (initialLoad) {
doSomething(change.doc.data());
} else {
doSomethingElse(change.doc.data());
}
});
@sle-c
sle-c / subscribe_to_activities_with_counter.js
Created September 6, 2018 18:32
Subscribe to firebase activities with counter
function handleActivitySubscription(snapshot) {
snapshot.docChanges().forEach(function(change) {
doSomething(change.doc.data());
});
}
const handleActivitySubscriptionWithCounter =
createFnCounter(handleActivitySubscription, 1);
db.collection("activities").where("userID", "==", currentUserID)
@sle-c
sle-c / subscribe_to_activities.js
Created September 6, 2018 18:29
subscribe to firebase activities
db.collection("activities").where("userID", "==", currentUserID)
.onSnapshot(function(snapshot) {
snapshot.docChanges().forEach(function(change) {
doSomething(change.doc.data());
});
});
@sle-c
sle-c / create_fn_counter.js
Last active June 10, 2020 00:13
Wrap a function and count how many time it's being invoked
function createFnCounter(fn, invokeBeforeExecution) {
let count = 0;
return (snapshot) => {
count++;
if (count <= invokeBeforeExecution) {
return null;
}
return fn(snapshot, count);
@sle-c
sle-c / config-react.md
Last active April 24, 2019 04:44
Create react app with webpack 4

Webpack config

Init project

mkdir project-folder
cd project-folder
npm init
@sle-c
sle-c / rails_uglifier_debug.rb
Created August 11, 2017 17:19
Debugging rails asset pipeline js precompile errors with uglifier
# runs rails console and copy + paste this snippet in to see the offending file
JS_PATH = "app/assets/javascripts/**/*.js";
Dir[JS_PATH].each do |file_name|
puts "\n#{file_name}"
puts Uglifier.compile(File.read(file_name))
end