Skip to content

Instantly share code, notes, and snippets.

Christopher Thornton cgthornt

Block or report user

Report or block cgthornt

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@cgthornt
cgthornt / hashing.cpp
Created Oct 25, 2018
Hash function in c++
View hashing.cpp
// Example:
//
// cout << hashString("hello world") << endl; // prints: 1128946856
#include <boost/functional/hash.hpp>
int hashString(const string& input) {
boost::hash<string> string_hash;
return string_hash(input);
}
View hashing.java
// Example:
//
// System.out.println(Hasher.sha1("hello world")); // prints: 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
// System.out.println(Hasher.sha1AsInt("hello world")); // prints: 896314922
import java.nio.charset.*;
import com.google.common.hash.*;
class Hasher {
public static String sha1(String input) {
@cgthornt
cgthornt / pkcs7_degenerate.rb
Last active Sep 12, 2017
Ruby PKCS#7 Certificate Only Degenerate (application/x-x509-ca-ra-cert)
View pkcs7_degenerate.rb
# Creates a degenerate PKCS#7 certificate only in ruby (SCEP application/x-x509-ca-ra-cert)
# Inspiration: https://github.com/AppBlade/TestHub/blob/master/app/controllers/scep_controller.rb#L92-L112
#
# Tested Ruby 2.2.0 OSX 10.10, OpenSSL 1.0.1l
require 'openssl'
cert = OpenSSL::X509::Certificate.new File.read('some_cert.crt')
# Fails ruby 2.2, OpenSSL 1.0.1l!!
p7certs = OpenSSL::PKCS7.new
@cgthornt
cgthornt / update_ghost.rb
Created Sep 10, 2014
Automatically Update Ghost Blog
View update_ghost.rb
#!/usr/bin/env ruby
#
# Automatically updates ghost per instructions at:
#
# http://support.ghost.org/how-to-upgrade/
#
# Make sure to back up your files before proceeding (i.e. git repo)!
#
require 'tempfile'
View sample_posts_controller_8.rb
class PostsController < ApiController
def create
@post = current_user.posts.create!(params.require(:post))
end
end
View sample_application_controller_3.rb
class ApiController < ActionController::Base
rescue_from ActionController::ParameterMissing do |e|
# You can even render a jbuilder template too!
render json: {error: e.message }, status: :unprocessable_entity
end
end
View sample_posts_controller_7.rb
class PostsController < ApiController
def update
@post = current_user.posts.find(params[:id])
@post.update_attributes!(params[:post])
end
def create
@post = current_user.posts.create!(params[:post])
end
end
View sample_application_controller_2.rb
class ApiController < ApplicationController::Base
rescue_from ActiveRecord::RecordNotFound do |e|
render json: { error: e.message }, status: :not_found
end
rescue_from ActiveRecord::RecordInvalid do |invalid|
# You can even use jbuilder templates to make this cleaner!
render 'shared/record_invalid', locals: { exception: invalid },
status: :unprocessable_entity
View sample_posts_controller_6.rb
class PostsController < ApiController
def update
@post = current_user.posts.find_by(id: params[:id])
# For error message
if !@post
return render json: { error: 'Unable to find blog post' }, status: :not_found
end
if !@post.update_attributes(param[:post])
return render json: {
error: "Could not update post",
View sample_posts_controller_5.rb
# app/views/posts/index.jbuilder
json.array! @posts, :id, :title, :description, :created_at, ...
You can’t perform that action at this time.