Skip to content

Instantly share code, notes, and snippets.

@ezekg
ezekg / electron-publisher-custom.js
Last active April 23, 2021 21:10
Custom electron-builder publisher for https://keygen.sh distribution service
/**
* NOTE(ezekg) Place this file in `resources/electron-publisher-custom.js`
*/
const { Publisher } = require('electron-publish')
const { log } = require('builder-util')
const { basename } = require('path')
const { stat } = require('fs-extra')
const { URL } = require('url')
const https = require('https')
const mime = require('mime')
@ezekg
ezekg / functions.php
Last active April 19, 2021 21:06
WooCommerce hook to generate a Keygen license key after a payment is completed
<?php
// Add these variables before running this code
$KEYGEN_PRODUCT_TOKEN = '...'
$KEYGEN_ACCOUNT_ID = '...'
$KEYGEN_POLICY_ID = '...'
// Add a hook after payment is completed
add_action('woocommerce_payment_complete', 'generate_license_key_after_payment_complete');

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@ezekg
ezekg / rsa.sh
Last active December 22, 2020 22:47
Verify signature using RSA-SHA256 w/ PKCS1-PSS padding using OpenSSL via the command line. For: https://github.com/keygen-sh/example-bash-cryptographic-verification
openssl dgst -verify public.pem -sha256 \
-sigopt rsa_padding_mode:pss \
-sigopt rsa_pss_saltlen:-2 \
-sigopt rsa_mgf1_md:sha256 \
-signature signature.bin \
data.txt
@ezekg
ezekg / keygen.vb
Last active December 29, 2020 19:49
Example of VB/VBA license key validation using https://keygen.sh and Visual Basic. If you're using this to add licensing an Excel document, you may need to activate "Microsoft Scripting Runtime" in Excel under "Tools" and then "References." See this tutorial on making HTTP requests from within Excel: http://excelerator.solutions/2017/08/28/excel…
' Call this function from a dialog that prompts for the customer's
' license key, or however you prefer. Call as often as needed.
Function ValidateLicenseKey(licenseKey As String)
' TODO: Update this to your Keygen account ID
Dim accountId As String
accountId = "demo"
Dim req As Object
req = CreateObject("MSXML2.XMLHTTP")
With req
@ezekg
ezekg / Program.cs
Created September 16, 2020 14:25
Example of validating a license and activating a machine with Keygen
using RestSharp;
using System;
using System.Collections.Generic;
class Keygen
{
public RestClient Client = null;
public Keygen(string accountId)
{
@ezekg
ezekg / twittermute.txt
Created January 24, 2020 22:12 — forked from IanColdwater/twittermute.txt
Here are some terms to mute on Twitter to clean your timeline up a bit.
Mute these words in your settings here: https://twitter.com/settings/muted_keywords
ActivityTweet
generic_activity_highlights
generic_activity_momentsbreaking
RankedOrganicTweet
suggest_activity
suggest_activity_feed
suggest_activity_highlights
suggest_activity_tweet
@ezekg
ezekg / _readme.md
Last active May 13, 2021 15:47
How to integrate 2Checkout with Keygen for license key creation. NOTE: Remember to set the following env vars: KEYGEN_PRODUCT_TOKEN, KEYGEN_ACCOUNT_ID and KEYGEN_POLICY_ID.

Usage

To generate a license and activation token, perform a GET request to the script, passing in an order query parameter.

curl -vg -X GET 'http://localhost:3000?order=1'

⚠️ Further Development ⚠️

@ezekg
ezekg / example-keygen-snipcart-php-integration.php
Last active May 13, 2021 15:49
How to integrate Snipcart with Keygen for license key creation.
<?php
// These are required environment variables!
define('KEYGEN_PRODUCT_TOKEN', getenv('KEYGEN_PRODUCT_TOKEN'));
define('KEYGEN_ACCOUNT_ID', getenv('KEYGEN_ACCOUNT_ID'));
define('KEYGEN_POLICY_ID', getenv('KEYGEN_POLICY_ID'));
define('SNIPCART_API_KEY', getenv('SNIPCART_API_KEY'));
// Function to validate that the webhook came from Snipcart
function validate_snipcart_webhook($data) {
<?php
// These are required!
$product_token = getenv('KEYGEN_PRODUCT_TOKEN');
$account_id = getenv('KEYGEN_ACCOUNT_ID');
// Retrieve the request's body and parse it as JSON
$body = @file_get_contents('php://input');
$event_json = json_decode($body);