Skip to content

Instantly share code, notes, and snippets.

View perry-mitchell's full-sized avatar

Perry Mitchell perry-mitchell

View GitHub Profile
@perry-mitchell
perry-mitchell / .editorconfig
Last active January 29, 2017 15:50
Lint/Editor config MASTER
root = true
[*]
indent_style = space
end_of_line = lf
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
@perry-mitchell
perry-mitchell / find-common-parent.js
Created January 10, 2017 13:40
Find common parent element of n nodes
/**
* Find common parent node
*/
export function findCommonParent(...nodes) {
let nodeParents = nodes.map(node => findParents(node)),
firstNodeParents = nodeParents[0],
topParent = firstNodeParents[0];
if (nodeParents.some(parents => parents.indexOf(topParent) !== 0)) {
// no common parent
@perry-mitchell
perry-mitchell / gatekeeper.js
Last active July 31, 2017 11:05
Javascript malicious redirect prevention script
(function () {
const CLICK_ALLOWANCE = 5000; // ms
const KEYCODE_F5 = 116;
const KEYCODE_LETTER_R = 82;
const watchedElements = [];
let allowanceTimer = null;
function arr(nodelist) {
@perry-mitchell
perry-mitchell / nightmare_preload.js
Last active August 28, 2017 06:43
Nightmare Preload
window.__nightmare = {};
__nightmare.ipc = require('electron').ipcRenderer;
__nightmare.sliced = require('sliced');
// Listen for error events
window.addEventListener('error', function(e) {
__nightmare.ipc.send('page', 'error', e.message, e.error.stack);
});
(function(){
@perry-mitchell
perry-mitchell / linda-dynamic-challenge.js
Created September 10, 2017 14:55
Linda Dynamic Challenge
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
const firstNames = ["Perry", "Linda", "Tom", "Noora", "Daniel"];
const lastNames = ["Mitchell", "Damario", "Jerry", "Suojansalo", "Bailo"];
function getName() {
const numFirstNames = firstNames.length;
const numLastNames = lastNames.length;
@perry-mitchell
perry-mitchell / package.json
Created September 19, 2017 05:36
Prettier on commit configuration
{
"name": "project",
"version": "0.0.0",
"scripts": {
"format": "prettier --tab-width 4 --print-width 120 --write 'source/**/*.js'",
"precommit": "lint-staged",
"test": "echo \"nothing yet\"",
"test:format": "prettier-check --tab-width 4 --print-width 120 'source/**/*.js'"
},
"lint-staged": {
@perry-mitchell
perry-mitchell / subtlecrypto-importkey.js
Created November 7, 2017 10:23
Import key with SubtleCrypto
subtleCrypto.importKey(
"raw",
stringToArrayBuffer(password),
{ name: "PBKDF2" },
false, // not extractable
["deriveBits"]
)
@perry-mitchell
perry-mitchell / encrypt-string.android.java
Created November 7, 2017 10:46
Encrypt a string in Java for Android
public static String encryptText(String text, String keyHex, String saltHex, String hmacHexKey) {
String ivHex = generateIV();
byte[] ivData = BCHelpers.hexStringToByteArray(ivHex);
byte[] keyData = BCHelpers.hexStringToByteArray(keyHex);
byte[] hmacKeyData = BCHelpers.hexStringToByteArray(hmacHexKey);
IvParameterSpec iv = new IvParameterSpec(ivData);
SecretKeySpec skeySpec = new SecretKeySpec(keyData, "AES");
try {
// AES encryption
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
@perry-mitchell
perry-mitchell / encrypt-string.objectivec.m
Created November 7, 2017 10:47
Encrypt a string in Objective-C
+ (NSString *)encryptText:(NSString *)text withKey:(NSString *)key andSalt:(NSString *)salt andHMAC:(NSString *)hmacHexKey {
// Validation
if (key.length != 64) {
return @"Error:Invalid key length";
} else if (hmacHexKey.length != 64) {
return @"Error:Invalid authentication information or possible tampering";
}
// Data prep
NSString *iv = [BCCrypto generateIVHex];
NSData *ivData = [BCHelpers dataFromHexString:iv];
RCT_EXPORT_METHOD(generateUUIDs:(RCTResponseSenderBlock)callback) {
NSArray *uuidArr = [BCCrypto generateUUIDsForCount:50];
callback(@[
[NSNull null],
[uuidArr componentsJoinedByString:@","]
]);
}