Skip to content

Instantly share code, notes, and snippets.

@RobinHAEVG
RobinHAEVG / pfx.go
Created January 7, 2025 09:15
Golang: get tls.Certificate from PFX certificate
func getTlsCertFromPkcs12(certFile, pw string) (tls.Certificate, error) {
bytes, err := os.ReadFile(certFile)
if err != nil {
return tls.Certificate{}, err
}
blocks, err := pkcs12.ToPEM(bytes, pw)
if err != nil {
return tls.Certificate{}, err
}
pemData := []byte{}
@RobinHAEVG
RobinHAEVG / Program.cs
Created September 12, 2024 12:58
Basic HTTP Listener in C#
using System.Net;
using System.Text;
namespace HttpListener;
internal class Program
{
static void Main(string[] args)
{
using var listener = new System.Net.HttpListener();
@RobinHAEVG
RobinHAEVG / local-network-ip.go
Created March 7, 2023 09:13
How to get the local network IP (not public IP)
conn, error: = net.Dial("udp", "8.8.8.8:80")
if error != nil {
fmt.Println(error)
}
defer conn.Close()
ipAddress: = conn.LocalAddr().( * net.UDPAddr)
@RobinHAEVG
RobinHAEVG / json.go
Created September 8, 2022 06:22
JSON Unmarshal difference?
// Difference between
var m myStruct
json.Unmarshal(data, &m)
// and
m := &myStruct{}
json.Unmarshal(data, m)
@RobinHAEVG
RobinHAEVG / main.go
Last active August 31, 2022 09:53
A tiny systray web application example
package main
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"runtime"
@RobinHAEVG
RobinHAEVG / unzip.go
Last active June 3, 2022 11:24
Zipping & Unzipping in Go
func UnzipFile(zipFile, targetFolder string) error {
r, err := zip.OpenReader(zipFile)
if err != nil {
return err
}
for _, f := range r.File {
fh, err := f.Open() // NOTE: mit Open kann man nur lesen
if err != nil {
return err
@RobinHAEVG
RobinHAEVG / decode-utf16-to-utf8.go
Created June 11, 2021 07:31
Decode UTF16 LE/BE BOM to UTF8
func decodeUTF16ToUTF8(b []byte, bigEndian bool) ([]byte, error) {
if len(b) % 2 != 0 {
return nil, fmt.Errorf("must have even length byte slice")
}
var (
u16s = make([]uint16, 1)
ret = &bytes.Buffer{}
b8buf = make([]byte, 4)
)
@RobinHAEVG
RobinHAEVG / https-server.go
Last active April 21, 2021 10:39
Golang Secure HTTPS Server
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
},
}
@RobinHAEVG
RobinHAEVG / embedded_file_helper.cs
Created October 30, 2020 12:01
Read embedded text-based files in C#
public static class Helper
{
private static readonly Dictionary<string, string> Cache = new Dictionary<string, string>();
public static string ReadUtf8EmbeddedRessource(string filename)
{
string utf8EmbeddedRessource;
lock (Cache)
{
@RobinHAEVG
RobinHAEVG / multitlsconfig.go
Last active October 19, 2020 12:00
Golang multiple TLS Config
package main
import (
"crypto/tls"
"log"
)
func main() {
var cert tls.Certificate // from wherever
tlsConfig := &tls.Config{