Skip to content

Instantly share code, notes, and snippets.

View manhnguyenv's full-sized avatar

Manh Nguyen manhnguyenv

View GitHub Profile
@manhnguyenv
manhnguyenv / UserWithCrypto.js
Created April 30, 2019 15:51 — forked from pdonham/UserWithCrypto.js
Sample code for Using JWTs for Authentication in RESTful Applications (http://sites.bu.edu/perryd/?p=259)
/*
For this version we'll add a register method to add a user to the database using proper
password encryption. It also demonstrates how to add and use a method on a schema.
Reference: https://www.sitepoint.com/user-authentication-mean-stack
*/
const mongoose = require('mongoose')
const crypto = require('crypto')
//const findOrCreate = require('mongoose-findorcreate')
//Set up ES6 Promises
@manhnguyenv
manhnguyenv / classes.js
Created April 5, 2019 09:08
pure javascript add class and remove class functions
var els = document.getElementsByClassName('current-class-name');
removeClass(els, 'current-class-name');
addClass(els, 'new-class-name');
var el = document.getElementById('current-class-name');
removeClass([el], 'current-class-name');
addClass([el], 'new-class-name');
function addClass(elements, className) {
for (var i = 0; i < elements.length; i++) {
@manhnguyenv
manhnguyenv / comaprison.cs
Created April 5, 2019 07:44 — forked from huseyint/comaprison.cs
Property equality comparison on two objects
// Shamelessly stolen and adapted from http://stackoverflow.com/questions/506096/comparing-object-properties-in-c
public static class Comparisons
{
public static bool PublicInstancePropertiesEqual<T>(this T self, T to, params string[] ignore) where T : class
{
if (self != null && to != null)
{
var type = typeof(T);
foreach (var propertyName in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Select(pi => pi.Name).Except(ignore))
{
@manhnguyenv
manhnguyenv / aes-example.cs
Created January 7, 2019 04:49 — forked from yetanotherchris/aes-example.cs
C# AES asymmetric encryption and decryption example
string ivAsBase64;
string encryptedTextAsBase64;
string keyAsBase64;
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
// Store the IV (they can be stored if you don't re-use a key)
aes.GenerateIV();
byte[] iv = aes.IV;
ivAsBase64 = Convert.ToBase64String(iv);
@manhnguyenv
manhnguyenv / slugify.js
Created July 26, 2018 04:00 — forked from mathewbyrne/slugify.js
Javascript Slugify
function slugify(text)
{
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
@manhnguyenv
manhnguyenv / slugify.js
Created July 26, 2018 04:00 — forked from codeguy/slugify.js
Create slug from string in Javascript
function string_to_slug (str) {
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
var to = "aaaaeeeeiiiioooouuuunc------";
for (var i=0, l=from.length ; i<l ; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}