Skip to content

Instantly share code, notes, and snippets.

Avatar

Dino Chiesa DinoChiesa

View GitHub Profile
@DinoChiesa
DinoChiesa / weightedRandomSelector2.js
Created Dec 2, 2021
perform a weighted random selection in JS
View weightedRandomSelector2.js
// weightedRandomSelector.js
//
// This is a simple weighted random selector, works in Rhino, can use within an
// Apigee JavaScript step.
//
// last saved: <2021-December-02 13:11:16>
//
// --------------------------------------------------------
(function (){
@DinoChiesa
DinoChiesa / convert.Java
Created Oct 8, 2021
Java: convert between ASN.1 and P1363 Encoding of Signature
View convert.Java
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERSequence;
import java.math.BigInteger;
private static byte[] toP1363(byte[] asn1EncodedSignature) throws Exception {
ASN1Sequence seq = ASN1Sequence.getInstance(asn1EncodedSignature);
BigInteger r = ((ASN1Integer) seq.getObjectAt(0)).getValue();
BigInteger s = ((ASN1Integer) seq.getObjectAt(1)).getValue();
@DinoChiesa
DinoChiesa / json-Stringify.js
Created Sep 29, 2021
diagnose JSON.stringify failure
View json-Stringify.js
/* jshint esversion:6, node:false, strict:implied */
/* global print, context */
function createPartialCopy(obj, partialPropertyList) {
// create new "partial copy" with only a subset of properties
return partialPropertyList.reduce(function(a, prop) {
a[prop] = obj[prop];
return a;
}, {});
}
@DinoChiesa
DinoChiesa / showcertssubjectandissuer
Created Aug 13, 2021
bash script, depends on openssl and perl, to show cert chain subject and issuer for a host. Runs on OS X
View showcertssubjectandissuer
#!/bin/bash
# uncomment to debug
# set -x
usage() {
printf "%s: display subject and issuer of certificates in the chain that a host presents\n" $0
printf "usage:\n"
printf " %s HOSTNAME\n" $0
printf " show cert subject and issuer\n\n"
@DinoChiesa
DinoChiesa / JS-ShredJSON.xml
Last active Jun 10, 2021
Shred JSON into context variables
View JS-ShredJSON.xml
<Javascript name='JS-ShredJSON' timeLimit='200' >
<Properties>
<Property name='output-prefix'>private</Property>
<Property name='source'>ContextVariableContainingJSON</Property>
</Properties>
<ResourceURL>jsc://extractJsonToContextVars.js</ResourceURL>
</Javascript>
@DinoChiesa
DinoChiesa / getTokenWithServiceAccount.js
Last active Apr 16, 2021
get a GCP token using a Service Account .json file
View getTokenWithServiceAccount.js
// getTokenWithServiceAccount.js
// ------------------------------------------------------------------
// uses only builtin modules, no external dependencies.
/* jshint esversion:9, node:true, strict:implied */
/* global process, console, Buffer */
const crypto = require('crypto'),
util = require('util'),
View invokeCallsWithLimit.js
// invokeCallsWithLimit.js
// ------------------------------------------------------------------
//
/* jshint esversion:6, node:false, strict:implied */
/* global properties, httpClient, context, Request */
const ASYNC_LIMIT = 3;
var start = Date.now();
View tweak-expiry-display.js
/* tweak-expiry-display.js */
/* jshint esversion:9, browser:true, strict:implied */
/* global window, navigator, console, fetch, Buffer */
/*
Instructions
Add this as a "custom script" in the "Settings" dialog for the portal.
Be sure to bracket this source with open-and-close script tags.
@DinoChiesa
DinoChiesa / 0-gcloud commands
Last active Jan 28, 2021
gcloud commands for interacting with Apigee, and also nodejs code to sign a JWT, for use in a request-for-token sent to Google APIs
View 0-gcloud commands
(First create a service account and create+download a keyfile for it)
KEY_FILE=~/Downloads/name-of-the-downloaded-keyfile.json
(download and install gcloud if you don't have it)
gcloud components update
gcloud auth list
View weightedRandomSelector.js
// weightedRandomSelector.js
//
// simple weighted random selector.
//
// Copyright © 2013, 2014 Dino Chiesa and Apigee Corp
// All rights reserved.
//
// created: Fri, 26 Jul 2013 11:14
// last saved: <2019-June-24 06:13:20>
//