Skip to content

Instantly share code, notes, and snippets.

View shimarin's full-sized avatar

Tomoatsu Shimada shimarin

View GitHub Profile
@shimarin
shimarin / AntiXSRFFilter.java
Last active August 29, 2015 13:55
A servlet filter which provides functionality supports AngularJS's XSRF protection scheme.
package com.walbrix.servlet;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
#!/usr/bin/ruby
require "fileutils"
COMPRESSION_SUFFIXES = {"gzip"=>".gz","bzip2"=>".bz2","lzma"=>".lzma","xz"=>".xz",nil=>""}
def receive_and_store(basename, suffix, destination, compression_program)
tmpfile = "#{destination}/00-#{basename}-in-progress"
if compression_program != nil
compression_suffix = COMPRESSION_SUFFIXES[compression_program]
--- invoice.js.old 2014-04-25 07:51:39.000000000 +0900
+++ invoice.js 2014-04-25 07:51:21.000000000 +0900
@@ -8,6 +8,7 @@
$scope.year = parseInt(data["番号"].substr(0,4));
$scope.month = parseInt(data["番号"].substr(4,2));
$scope.day = parseInt(data["番号"].substr(6,2));
- $scope.taxPercent = 5;
+ $scope.taxPercent = 8;
+ if ($scope.year * 100 + $scope.month < 201404) $scope.taxPercent = 5;
$scope.tax = Math.floor($scope.minorTotal * $scope.taxPercent / 100);
@shimarin
shimarin / Gruntfile.js
Last active August 29, 2015 14:02
HTMLファイルをAngularJSのモジュールに変換する Gruntfile
module.exports = function(grunt) {
grunt.initConfig({
ngtemplates: {
app: {
src: "*.html",
dest: "template.js"
}
}
});
grunt.loadNpmTasks('grunt-angular-templates');
@shimarin
shimarin / SqlDateTimeSerializerModule.scala
Created June 7, 2014 17:20
Ajaxアプリケーションにミリ秒やタイムゾーン付き日付時刻文字列で日付時刻を渡すとサーバとクライアントの間に時差がある時に意図に反して時刻が正規化されてしまうことがあるのでjava.sql系の日付時刻型を強制的にタイムゾーンなしのテキスト表現にしてしまうJackson用シリアライザーモジュール
package com.walbrix.jackson
import com.fasterxml.jackson.databind.JsonSerializer
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.core.Version
import java.util.Calendar
import java.sql.Date
import java.sql.Time
@shimarin
shimarin / Foldleft.scala
Last active August 29, 2015 14:02
SQLあるある
val src = Seq(
(1, "shimariso", 1),
(1, "shimariso", 2),
(1, "shimariso", 3),
(2, "sorimashi", 4),
(2, "sorimashi", 5),
(2, "sorimashi", 6)
) // SQLで一対多関係にあるふたつのテーブルをJOINしていっぺんに持ってきたと思いねえ(IDでorder by済み)
case class Result(id:Int, name:String, nums:Seq[Int])
import com.nimbusds.jose.Payload
import com.nimbusds.jose.JWSHeader
import com.nimbusds.jose.JWSAlgorithm
import com.nimbusds.jose.JWSObject
import com.nimbusds.jose.crypto.MACSigner
import com.nimbusds.jose.crypto.MACVerifier
object NimbusJoseJwt {
def main(args: Array[String]): Unit = {
// Create JWS payload
@shimarin
shimarin / JWT.scala
Created June 10, 2014 14:49
任意のデータをJSON Web Tokenでエンコードしたり、それをデコードしたりする最短のコード
import scala.collection.JavaConversions._
import com.nimbusds.jose.{JWEHeader,EncryptionMethod,JWEAlgorithm,JOSEException}
import com.nimbusds.jose.crypto.{DirectEncrypter,DirectDecrypter}
import com.nimbusds.jwt.{JWTClaimsSet,EncryptedJWT,ReadOnlyJWTClaimsSet}
import java.text.ParseException
object JWT {
val sharedKey = "GLU9nOFeRfBUy89d".getBytes /* 適当な暗号キー(共通鍵なので要秘匿) */
@shimarin
shimarin / auth-by-chap-secrets.py
Last active August 29, 2015 14:04
PPPのchap-secretsファイルに書かれているパスワードでOpenVPNの認証をするためのPythonスクリプト
#!/usr/bin/python
import os,sys,re
users = {}
def process_line(line):
if line.startswith('#'): return
line_splitted = re.split(r'\s', line)
if len(line_splitted) < 3: return
users[line_splitted[0]] = line_splitted[2]
package main
// #include <stdio.h>
import "C"
func main() {
C.puts(C.CString("Hello, World!"))
}