Skip to content

Instantly share code, notes, and snippets.

@WalterInSH
WalterInSH / add-ssh-key.sh
Created July 11, 2016 04:37
add a ssh key on servers
#!/bin/sh
SSH_KEY="key"
if [ -f ~/.ssh/authorized_keys ] && [ $(grep -c "$SSH_KEY" ~/.ssh/authorized_keys) -gt 0 ]; then
echo "ssh key exists"
else
if [ -f ~/.ssh/authorized_keys ]; then
cp ~/.ssh/authorized_keys ~/.ssh/authorized_keys.bak
@WalterInSH
WalterInSH / angularjs_watch_list
Created July 14, 2015 10:44
angularjs watch list. avoid duplicated watching
$scope.watcher_switches = [];
$scope.add_status_watcher = function () {
//stop watching
angular.forEach($scope.watcher_switches, function (value) {
value();
});
//bind new watchers
var new_watchers = [];
@WalterInSH
WalterInSH / angular-http-error-handler.js
Created May 25, 2015 07:19
angular http error handler
/**
* AngularJS v1.3.15
*/
var app = angular.module('fds-punisher', modules, function ($routeProvider, $locationProvider, $httpProvider) {
$httpProvider.interceptors.push(function($q) {
return {
'responseError': function(rejection) {
var status = rejection.status;
if (status == 400) {
/**
* User: walter
* Date: 5/5/14
* Time: 5:09 PM
*/
public class StringFormatter {
static final String DELIM_STR = "{}";
/**
public static long getCRC32(String _source) throws UnsupportedEncodingException {
int crc = 0xFFFFFFFF; // initial contents of LFBSR
int poly = 0xEDB88320; // reverse polynomial
byte[] bytes = _source.getBytes("utf-8");
for (byte b : bytes) {
int temp = (crc ^ b) & 0xff;
// read 8 bits one at a time
for (int i = 0; i < 8; i++) {
if ((temp & 1) == 1)
@WalterInSH
WalterInSH / RSA.java
Created February 20, 2014 09:27
RSA algorithm
package me.faolou.util;
/*************************************************************************
* Compilation: javac RSA.java
* Execution: java RSA N
*
* Generate an N-bit public and private RSA key and use to encrypt
* and decrypt a random message.
*
*
@WalterInSH
WalterInSH / MillerRabin
Created February 20, 2014 05:48
Miller Rabin primality test
private boolean passesMillerRabin(int iterations, Random rnd) {
// Find a and m such that m is odd and this == 1 + 2**a * m
BigInteger thisMinusOne = this.subtract(ONE);
BigInteger m = thisMinusOne;
int a = m.getLowestSetBit();
m = m.shiftRight(a);
// Do the tests
if (rnd == null) {
rnd = getSecureRandom();
@WalterInSH
WalterInSH / Pagination.java
Last active November 27, 2020 23:01
pagination
import org.codehaus.jackson.annotate.JsonIgnore;
/**
* User: walter
* Date: 12/8/13
* Time: 10:53 AM
*/
public class Pagination {
// 每页显示数量
@WalterInSH
WalterInSH / ExcelUtils.java
Last active November 24, 2020 02:58
generate excel dynamically
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@WalterInSH
WalterInSH / logback.xml
Created February 8, 2014 06:27
logback configuration
<?xml version="1.0" encoding="UTF-8"?>
<!-- <configuration debug="true"> 调试模式下,可输出logback的内部日志信息 -->
<configuration debug="false">
<!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 [%file:%line]日志所在文件及行数 %msg%n消息及换行-->
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%level][%thread]:%logger{50} [%method:%line] %msg%n</pattern>