Skip to content

Instantly share code, notes, and snippets.

View chrisvoo's full-sized avatar
🎸
hard as a rock

Christian Castelli chrisvoo

🎸
hard as a rock
View GitHub Profile
@chrisvoo
chrisvoo / getFileHash.js
Created February 19, 2019 19:00
Node.js: function to create an hash from a file (path or Buffer)
const crypto = require('crypto');
const fs = require('fs');
/**
* It calculates the hash of a file
* @param {string} hashName type of hash (md5, sha1, etc)
* @param {string|Stream} file a file's path or a readable stream
* if it's been already opened somewhere else
*/
function getFileHash(hashName, file) {
@chrisvoo
chrisvoo / flac_to_mp3.sh
Last active December 10, 2022 20:03
Converts a FLAC file to MP3 with ffmpeg and lame encoder
to_mp3() {
filename=$(basename "$1")
extension="${filename##*.}"
filename="${filename%.*}"
ffmpeg -i "$1" -codec:a libmp3lame -b:a 320k -map_metadata 0 -id3v2_version 3 ${filename}.mp3
}
@chrisvoo
chrisvoo / email_test.php
Created December 2, 2015 10:04
Testing external SMTP server with PHP
<?php
# use composer : php composer.phar require swiftmailer/swiftmailer @stable
require __DIR__ . '/vendor/autoload.php';
// Approach 1: Change the global setting (suggested)
Swift_Preferences::getInstance()->setCharset('utf-8');
$smtp_host = '1.2.3.4';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance($smtp_host, 25);
// Create the Mailer using your created Transport
@chrisvoo
chrisvoo / fluent.java
Last active September 23, 2016 00:48
GET/POST HTTP JSON request with Apache Fluent library (libphonenumber Google library)
/**
* Apache HTTP Fluent client configuration
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
* @throws IOException
* @throws CertificateException
*/
public static void httpClientConfigurator() throws KeyStoreException, KeyManagementException, NoSuchAlgorithmException,
CertificateException, IOException {
@chrisvoo
chrisvoo / bcrypt.txt
Last active October 6, 2016 04:12
BCrypt implementations
/* $2$(2 chars work)$(22 chars salt)(31 chars hash)
* 2 - the original BCrypt, which has been deprecated because of a security issue a long time before BCrypt became popular.
* 2a - the official BCrypt algorithm and a insecure implementation in crypt_blowfish
* 2x - suggested for hashes created by the insecure algorithm for compatibility
* 2y - suggested new marker for the fixed crypt_blowfish
*
* So 2a hashes created by the original algorithm or the java port are fine, and identical to 2y-hashes created by
* crypt_blowfish. But 2a hashes created by crypt_blowfish are insecure. */
@chrisvoo
chrisvoo / jsoup_examples.java
Last active January 12, 2021 09:50
GET/POST HTTP request and HTML parsing with Jsoup library
import java.io.IOException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@chrisvoo
chrisvoo / pgsql_administration.sh
Last active October 6, 2016 04:13
psql/pg_dump administration commands
# dump of your database in plain text. It creates tables, views, etc
# for exporting multiple tables only, you can repeat parameter -t
pg_dump --host <IP> --port 5432 --username "myuser" \
--format plain --create --encoding UTF8 -v \
--file "/my/path/file.sql" "db_name"
# or, with short opts and wrapping all SQL statements inside a transaction with automatic ROLLBACK
psql -U postgres -p 5433 --single-transaction -f /my/path/file.sql db_name
# Import of a SQL dump. Be sure to have write permissions in the current directory
@chrisvoo
chrisvoo / pgsql_cheatsheet.sql
Last active September 19, 2022 03:44
PostgreSQL cheat sheet
-- QUERIES OPERATIONS
----------------------------------------------------------------------------------
-- KILL ALL sessions FOR A DATABASE
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB'
AND pid <> pg_backend_pid();
-- GETTING RUNNING QUERIES, version: 9.2+