Skip to content

Instantly share code, notes, and snippets.

@lifeisfoo
lifeisfoo / mongo-regex-update-array.js
Created November 18, 2016 11:29
Update mongodb array values with a regex - WARNING: clear but inefficient code
/*
Product {
//...
images: [String] //could be undefined
}
*/
db.getCollection('products').find().forEach(function (doc) {
if (doc.images !== undefined) {
for(var i = 0; i < doc.images.length; i++) {
@lifeisfoo
lifeisfoo / dockercloud-ops.sh
Last active November 20, 2016 16:37
dockerc-cloud cli common operations
# dcs = docker-cloud service, see https://github.com/lifeisfoo/docker-cloud-aliases
dcs create -n service-name -t 1 image-name:tag
dcs start service-name # or UUID
dcs env ls b239 # show environment variables for a service
dcs set --link-service mongo:mongo b239 # link another service to this
dcs env add -e DATABASE_HOST=mongo b239 # add a variable to the env
dcs env add -e DATABASE_PORT=27017 b239 # add another variable
@lifeisfoo
lifeisfoo / Dockerfile
Created November 5, 2016 10:18
docker jre8 wkhtmltopdf alpine
FROM openjdk:8u92-jre-alpine
RUN apk add --update --no-cache \
curl libgcc libstdc++ libx11 glib libxrender libxext libintl ttf-liberation
RUN curl -O http://download.gna.org/wkhtmltopdf/0.12/0.12.3/wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
RUN tar -xvJ -f wkhtmltox-0.12.3_linux-generic-amd64.tar.xz wkhtmltox/bin/wkhtmltopdf
RUN mv wkhtmltox/bin/wkhtmltopdf /usr/bin
RUN rm -rf wkhtmltox
RUN rm wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
@lifeisfoo
lifeisfoo / RatpackMongoGORM.groovy
Created November 3, 2016 20:33
Quick start with Ratpack, Mongo and GORM in Groovy
/*
* http://www.groovy-lang.org/download.html
* https://www.mongodb.com/download-center?jmp=nav#community
* https://ratpack.io/manual/current/all.html
* http://gorm.grails.org/latest/mongodb/manual/
*/
@Grapes([
@Grab('io.ratpack:ratpack-groovy:1.4.4'),
@Grab('org.slf4j:slf4j-simple:1.7.21'),
@Grab('org.grails:grails-datastore-gorm-mongodb:6.0.3.RELEASE'),
@lifeisfoo
lifeisfoo / NSURLSessionPinningDelegate.swift
Created March 3, 2016 18:47
Swift version of OWASP's didReceiveAuthenticationChallenge method (for ios SSL PINNING) (complete method http://stackoverflow.com/a/34223292/3340702)
import Foundation
import Security
class NSURLSessionPinningDelegate: NSObject, NSURLSessionDelegate {
func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
// Adapted from OWASP https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning#iOS
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
@lifeisfoo
lifeisfoo / generate_bisect_repo.sh
Last active February 4, 2016 23:10
A script that generate a repository with random commits and one bug (return code) in one of them
#!/bin/sh
mkdir git-bisect-test
cd git-bisect-test
git init
echo "#!bin/sh\n" > script.sh
echo "exit 0" >> script.sh
git add script.sh
git commit -a -m "initial commit"
@lifeisfoo
lifeisfoo / MyViewController.swift
Last active July 9, 2016 22:35
iOS CNPPopupController Swift example
//
// Swift CNPPopupController ViewController Example
// See https://github.com/carsonperrotti/CNPPopupController
//
// Created by Alessandro Miliucci on 7 Jan 2016.
// lifeisfoo@gmail.com
//
import UIKit
import CNPPopupController
#!/bin/bash
###############################################################################
## ##
## Build and package OpenSSL static libraries for OSX/iOS ##
## ##
## This script is in the public domain. ##
## Creator : Laurent Etiemble ##
## ##
###############################################################################
@lifeisfoo
lifeisfoo / NSData+Encryption.h
Last active August 29, 2015 14:19
iOS AES-256 encryption (headers)
// See for converting from e to NSString: http://ios-blog.co.uk/tutorials/quick-tips/quick-tip-converting-nsstring-to-nsdata/
@interface NSData (Encryption)
- (NSData *)AES256EncryptWithKey:(NSString *)key;
- (NSData *)AES256DecryptWithKey:(NSString *)key;
@end
@lifeisfoo
lifeisfoo / NSData+Encryption.m
Created April 22, 2015 14:56
iOS AES-256 encryption
#import "NSData+Encryption.h"
#import <CommonCrypto/CommonCryptor.h>
@implementation NSData (Encryption)
- (NSData *)AES256EncryptWithKey:(NSString *)key {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data