Skip to content

Instantly share code, notes, and snippets.

@oharsta
oharsta / IPvXPrefixCalculator.scala
Last active August 29, 2015 14:07
Calculate the addition of IPv4 and IPv6 prefix lengths (e.g. a IPv4 /23 and a /23 result in a /22)
object PrefixCalculator {
def sumIpv4(ipv4Prefixes: Seq[Int]) : Int = doSumIpvX(ipv4Prefixes, 32)
def sumIpv6(ipv6Prefixes: Seq[Int]) : Int = doSumIpvX(ipv6Prefixes, 128)
private def doSumIpvX(prefixes: Seq[Int], bitSize: Int) : Int = {
val addressSize = prefixes.map(prefix => Math.pow(2, bitSize - prefix)).sum
(bitSize - (Math.log(addressSize) / Math.log(2))).floor.toInt
}
'use strict';
angular.module('portal')
.factory('PrefixCalculator', function () {
/**
* Sums up the prefixLengths given a specified bitSize
*
* @param prefixes prefixLength integers (e.g. 22, 39 etc)
* @param bitSize the bitSize of the IP (e.g. 32 or 128)
@oharsta
oharsta / AngularNonLatin1
Created January 31, 2015 06:15
Angular Validate non latin-1 chars
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form latin-1 input validation</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js"></script>
<script type="text/javascript">
(function(angular) {
'use strict';
@oharsta
oharsta / JsonMapper.java
Created March 27, 2016 15:46
Inject singleton instance into Java (+8) classes
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
public interface JsonMapper {
ObjectMapper mapper = ObjectMapperWrapper.init();
class ObjectMapperWrapper {
private static com.fasterxml.jackson.databind.ObjectMapper init() {
@oharsta
oharsta / StopWatchAdvice.java
Created March 31, 2016 15:56
StopWatch - without AspectJ - for every request (do not use in production)
package aa.web;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.filter.OncePerRequestFilter;
@oharsta
oharsta / EduGainFeedParser.java
Last active May 15, 2016 15:27
Parsing a large XML SAML feed of IdentityProviders and ServiceProviders to a Map with all Service Providers entityID's and the signing certificates
package eduproxy.saml;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import java.util.HashMap;
@oharsta
oharsta / KeyStoreService.java
Created May 27, 2016 11:23
Add certificates and private keys to an existing or new KeyStore in Java or create a self-signed certificate and add this.
import org.apache.commons.io.IOUtils;
import sun.security.tools.keytool.CertAndKeyGen;
import sun.security.x509.X500Name;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
@oharsta
oharsta / I18n.test.js
Created May 17, 2017 20:04
Verifying every I18n translation is present in all translations
import I18n from "i18n-js";
import en from "../locale/en";
import nl from "../locale/nl";
expect.extend({
toContainKey(translation, key) {
const pass = (translation[key] !== undefined);
return {
message: () => `Expected ${key} to be present in ${JSON.stringify(translation)}`,
pass: pass
@oharsta
oharsta / KeyGenerator.java
Created November 26, 2019 07:53
PEM encoded RSA private key with certificate
package myconext.crypto;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.cert.CertIOException;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.jcajce.JcaPEMWriter;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.OperatorCreationException;
@oharsta
oharsta / babel.config.js
Created March 28, 2020 15:56
IE11 Svelte Webpack configuration
module.exports = {
presets: [
[
'@babel/preset-env',
{
debug: true,
"corejs": 3,
"useBuiltIns": "entry",
targets: ['last 2 versions', 'ie >= 11']
}],