initial commit
This commit is contained in:
commit
d5385fbb7f
|
@ -0,0 +1,366 @@
|
|||
# Created by https://www.gitignore.io/api/intellij,go,linux,osx,windows,node,python,executable,jetbrains+all,visualstudiocode,compressedarchive,git
|
||||
# Edit at https://www.gitignore.io/?templates=intellij,go,linux,osx,windows,node,python,executable,jetbrains+all,visualstudiocode,compressedarchive,git
|
||||
|
||||
### CompressedArchive ###
|
||||
|
||||
### Mostly from https://en.wikipedia.org/wiki/List_of_archive_formats
|
||||
|
||||
## Archiving and compression
|
||||
# Open source file format. Used by 7-Zip.
|
||||
*.7z
|
||||
# Mac OS X, restoration on different platforms is possible although not immediate Yes Based on 7z. Preserves Spotlight metadata, resource forks, owner/group information, dates and other data which would be otherwise lost with compression.
|
||||
*.s7z
|
||||
# Old archive versions only Proprietary format
|
||||
*.ace
|
||||
# A format that compresses and doubly encrypt the data (AES256 and CAS256) avoiding brute force attacks, also hide files in an AFA file. It has two ways to safeguard data integrity and subsequent repair of the file if has an error (repair with AstroA2P (online) or Astrotite (offline)).
|
||||
*.afa
|
||||
# A mainly Korean format designed for very large archives.
|
||||
*.alz
|
||||
# Android application package (variant of JAR file format).
|
||||
*.apk
|
||||
# ??
|
||||
*.arc
|
||||
# Originally DOS, now multiple
|
||||
*.arj
|
||||
# Open archive format, used by B1 Free Archiver (http://dev.b1.org/standard/archive-format.html)
|
||||
*.b1
|
||||
# Binary Archive with external header
|
||||
*.ba
|
||||
# Proprietary format from the ZipTV Compression Components
|
||||
*.bh
|
||||
# The Microsoft Windows native archive format, which is also used by many commercial installers such as InstallShield and WISE.
|
||||
*.cab
|
||||
# Originally DOS, now DOS and Windows Created by Yaakov Gringeler; released last in 2003 (Compressia 1.0.0.1 beta), now apparently defunct. Free trial of 30 days lets user create and extract archives; after that it is possible to extract, but not to create.
|
||||
*.car
|
||||
# Open source file format.
|
||||
*.cfs
|
||||
# Compact Pro archive, a common archiver used on Mac platforms until about Mac OS 7.5.x. Competed with StuffIt; now obsolete.
|
||||
*.cpt
|
||||
# Windows, Unix-like, Mac OS X Open source file format. Files are compressed individually with either gzip, bzip2 or lzo.
|
||||
*.dar
|
||||
# DiskDoubler Mac OS obsolete
|
||||
*.dd
|
||||
# ??
|
||||
*.dgc
|
||||
# Apple Disk Image upports "Internet-enabled" disk images, which, once downloaded, are automatically decompressed, mounted, have the contents extracted, and thrown away. Currently, Safari is the only browser that supports this form of extraction; however, the images can be manually extracted as well. This format can also be password-protected or encrypted with 128-bit or 256-bit AES encryption.
|
||||
*.dmg
|
||||
# Enterprise Java Archive archive
|
||||
*.ear
|
||||
# ETSoft compressed archive
|
||||
*.egg
|
||||
# The predecessor of DGCA.
|
||||
*.gca
|
||||
# Originally DOS Yes, but may be covered by patents DOS era format; uses arithmetic/Markov coding
|
||||
*.ha
|
||||
# MS Windows HKI
|
||||
*.hki
|
||||
# Produced by ICEOWS program. Excels at text file compression.
|
||||
*.ice
|
||||
# Java archive, compatible with ZIP files
|
||||
*.jar
|
||||
# Open sourced archiver with compression using the PAQ family of algorithms and optional encryption.
|
||||
*.kgb
|
||||
# Originally DOS, now multiple Multiple Yes The standard format on Amiga.
|
||||
*.lzh
|
||||
*.lha
|
||||
# Archiver originally used on The Amiga. Now copied by Microsoft to use in their .cab and .chm files.
|
||||
*.lzx
|
||||
# file format from NoGate Consultings, a rival from ARC-Compressor.
|
||||
*.pak
|
||||
# A disk image archive format that supports several compression methods as well as splitting the archive into smaller pieces.
|
||||
*.partimg
|
||||
# An experimental open source packager (http://mattmahoney.net/dc)
|
||||
*.paq*
|
||||
# Open source archiver supporting authenticated encryption, volume spanning, customizable object level and volume level integrity checks (form CRCs to SHA-512 and Whirlpool hashes), fast deflate based compression
|
||||
*.pea
|
||||
# The format from the PIM - a freeware compression tool by Ilia Muraviev. It uses an LZP-based compression algorithm with set of filters for executable, image and audio files.
|
||||
*.pim
|
||||
# PackIt Mac OS obsolete
|
||||
*.pit
|
||||
# Used for data in games written using the Quadruple D library for Delphi. Uses byte pair compression.
|
||||
*.qda
|
||||
# A proprietary archive format, second in popularity to .zip files.
|
||||
*.rar
|
||||
# The format from a commercial archiving package. Odd among commercial packages in that they focus on incorporating experimental algorithms with the highest possible compression (at the expense of speed and memory), such as PAQ, PPMD and PPMZ (PPMD with unlimited-length strings), as well as a proprietary algorithms.
|
||||
*.rk
|
||||
# Self Dissolving ARChive Commodore 64, Commodore 128 Commodore 64, Commodore 128 Yes SDAs refer to Self Dissolving ARC files, and are based on the Commodore 64 and Commodore 128 versions of ARC, originally written by Chris Smeets. While the files share the same extension, they are not compatible between platforms. That is, an SDA created on a Commodore 64 but run on a Commodore 128 in Commodore 128 mode will crash the machine, and vice versa. The intended successor to SDA is SFX.
|
||||
*.sda
|
||||
# A pre-Mac OS X Self-Extracting Archive format. StuffIt, Compact Pro, Disk Doubler and others could create .sea files, though the StuffIt versions were the most common.
|
||||
*.sea
|
||||
# Scifer Archive with internal header
|
||||
*.sen
|
||||
# Commodore 64, Commodore 128 SFX is a Self Extracting Archive which uses the LHArc compression algorithm. It was originally developed by Chris Smeets on the Commodore platform, and runs primarily using the CS-DOS extension for the Commodore 128. Unlike its predecessor SDA, SFX files will run on both the Commodore 64 and Commodore 128 regardless of which machine they were created on.
|
||||
*.sfx
|
||||
# An archive format designed for the Apple II series of computers. The canonical implementation is ShrinkIt, which can operate on disk images as well as files. Preferred compression algorithm is a combination of RLE and 12-bit LZW. Archives can be manipulated with the command-line NuLib tool, or the Windows-based CiderPress.
|
||||
*.shk
|
||||
# A compression format common on Apple Macintosh computers. The free StuffIt Expander is available for Windows and OS X.
|
||||
*.sit
|
||||
# The replacement for the .sit format that supports more compression methods, UNIX file permissions, long file names, very large files, more encryption options, data specific compressors (JPEG, Zip, PDF, 24-bit image, MP3). The free StuffIt Expander is available for Windows and OS X.
|
||||
*.sitx
|
||||
# A royalty-free compressing format
|
||||
*.sqx
|
||||
# The "tarball" format combines tar archives with a file-based compression scheme (usually gzip). Commonly used for source and binary distribution on Unix-like platforms, widely available elsewhere.
|
||||
*.tar.gz
|
||||
*.tgz
|
||||
*.tar.Z
|
||||
*.tar.bz2
|
||||
*.tbz2
|
||||
*.tar.lzma
|
||||
*.tlz
|
||||
# UltraCompressor 2.3 was developed to act as an alternative to the then popular PKZIP application. The main feature of the application is its ability to create large archives. This means that compressed archives with the UC2 file extension can hold almost 1 million files.
|
||||
*.uc
|
||||
*.uc0
|
||||
*.uc2
|
||||
*.ucn
|
||||
*.ur2
|
||||
*.ue2
|
||||
# Based on PAQ, RZM, CSC, CCM, and 7zip. The format consists of a PAQ, RZM, CSC, or CCM compressed file and a manifest with compression settings stored in a 7z archive.
|
||||
*.uca
|
||||
# A high compression rate archive format originally for DOS.
|
||||
*.uha
|
||||
# Web Application archive (Java-based web app)
|
||||
*.war
|
||||
# File-based disk image format developed to deploy Microsoft Windows.
|
||||
*.wim
|
||||
# XAR
|
||||
*.xar
|
||||
# Native format of the Open Source KiriKiri Visual Novel engine. Uses combination of block splitting and zlib compression. The filenames and pathes are stored in UTF-16 format. For integrity check, the Adler-32 hashsum is used. For many commercial games, the files are encrypted (and decoded on runtime) via so-called "cxdec" module, which implements xor-based encryption.
|
||||
*.xp3
|
||||
# Yamazaki zipper archive. Compression format used in DeepFreezer archiver utility created by Yamazaki Satoshi. Read and write support exists in TUGZip, IZArc and ZipZag
|
||||
*.yz1
|
||||
# The most widely used compression format on Microsoft Windows. Commonly used on Macintosh and Unix systems as well.
|
||||
*.zip
|
||||
*.zipx
|
||||
# application/x-zoo zoo Multiple Multiple Yes
|
||||
*.zoo
|
||||
# Journaling (append-only) archive format with rollback capability. Supports deduplication and incremental update based on last-modified dates. Multi-threaded. Compresses in LZ77, BWT, and context mixing formats. Open source.
|
||||
*.zpaq
|
||||
# Archiver with a compression algorithm based on the Burrows-Wheeler transform method.
|
||||
*.zz
|
||||
|
||||
|
||||
### Executable ###
|
||||
*.app
|
||||
*.bat
|
||||
*.cgi
|
||||
*.com
|
||||
*.exe
|
||||
*.gadget
|
||||
*.pif
|
||||
*.vb
|
||||
*.wsf
|
||||
|
||||
### Git ###
|
||||
# Created by git for backups. To disable backups in Git:
|
||||
# $ git config --global mergetool.keepBackup false
|
||||
*.orig
|
||||
|
||||
# Created by git when using merge tools for conflicts
|
||||
*.BACKUP.*
|
||||
*.BASE.*
|
||||
*.LOCAL.*
|
||||
*.REMOTE.*
|
||||
*_BACKUP_*.txt
|
||||
*_BASE_*.txt
|
||||
*_LOCAL_*.txt
|
||||
*_REMOTE_*.txt
|
||||
|
||||
### Go ###
|
||||
# Binaries for programs and plugins
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
### Go Patch ###
|
||||
/vendor/
|
||||
/Godeps/
|
||||
|
||||
### Intellij ###
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/**/usage.statistics.xml
|
||||
.idea/**/dictionaries
|
||||
.idea/**/shelf
|
||||
|
||||
# Generated files
|
||||
.idea/**/contentModel.xml
|
||||
|
||||
# Sensitive or high-churn files
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
.idea/**/dbnavigator.xml
|
||||
|
||||
# Gradle
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
cmake-build-*/
|
||||
|
||||
# Mongo Explorer plugin
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
# File-based project format
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
### Intellij Patch ###
|
||||
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
|
||||
|
||||
# *.iml
|
||||
# modules.xml
|
||||
# .idea/misc.xml
|
||||
# *.ipr
|
||||
|
||||
# Sonarlint plugin
|
||||
.idea/sonarlint
|
||||
|
||||
### JetBrains+all Patch ###
|
||||
# Ignores the whole .idea folder and all .iml files
|
||||
# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360
|
||||
|
||||
.idea/
|
||||
|
||||
# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023
|
||||
|
||||
*.iml
|
||||
modules.xml
|
||||
.idea/misc.xml
|
||||
*.ipr
|
||||
|
||||
# Sonarlint plugin
|
||||
|
||||
### Linux ###
|
||||
*~
|
||||
|
||||
# temporary files which can be created if a process still has a handle open of a deleted file
|
||||
.fuse_hidden*
|
||||
|
||||
# KDE directory preferences
|
||||
.directory
|
||||
|
||||
# Linux trash folder which might appear on any partition or disk
|
||||
.Trash-*
|
||||
|
||||
# .nfs files are created when an open file is removed but is still being accessed
|
||||
.nfs*
|
||||
|
||||
### OSX ###
|
||||
# General
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Icon must end with two \r
|
||||
Icon
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
# Files that might appear in the root of a volume
|
||||
.DocumentRevisions-V100
|
||||
.fseventsd
|
||||
.Spotlight-V100
|
||||
.TemporaryItems
|
||||
.Trashes
|
||||
.VolumeIcon.icns
|
||||
.com.apple.timemachine.donotpresent
|
||||
|
||||
# Directories potentially created on remote AFP share
|
||||
.AppleDB
|
||||
.AppleDesktop
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
|
||||
### VisualStudioCode ###
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
|
||||
### VisualStudioCode Patch ###
|
||||
# Ignore all local history of files
|
||||
.history
|
||||
|
||||
### Windows ###
|
||||
# Windows thumbnail cache files
|
||||
Thumbs.db
|
||||
Thumbs.db:encryptable
|
||||
ehthumbs.db
|
||||
ehthumbs_vista.db
|
||||
|
||||
# Dump file
|
||||
*.stackdump
|
||||
|
||||
# Folder config file
|
||||
[Dd]esktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Windows Installer files
|
||||
*.msi
|
||||
*.msix
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# Windows shortcuts
|
||||
*.lnk
|
||||
|
||||
# End of https://www.gitignore.io/api/intellij,go,linux,osx,windows,node,python,executable,jetbrains+all,visualstudiocode,compressedarchive,git
|
||||
|
||||
go.sum
|
||||
/vendor
|
|
@ -0,0 +1,22 @@
|
|||
The BSD 3-Clause License
|
||||
|
||||
Copyright (c) 2022 Sangbum Kim.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided
|
||||
that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions
|
||||
and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
|
||||
the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
|
||||
promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@ -0,0 +1,20 @@
|
|||
# miscellaneous
|
||||
|
||||
[](http://godoc.org/amuz.es/src/go/misc)
|
||||
[](https://goreportcard.com/report/amuz.es/src/go/misc)
|
||||
|
||||
## Description
|
||||
|
||||
Miscellaneous utility collections.
|
||||
|
||||
## Requirements
|
||||
|
||||
Go 1.5 or above.
|
||||
|
||||
## Installation
|
||||
|
||||
Run the following command to install the package:
|
||||
|
||||
```
|
||||
go get amuz.es/src/go/misc
|
||||
```
|
|
@ -0,0 +1,63 @@
|
|||
package backoff
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Algorithm defines a function that calculates a time.Duration based on
|
||||
// the given retry attempt number.
|
||||
type Algorithm func(attempt uint) time.Duration
|
||||
|
||||
// Incremental creates a Algorithm that increments the initial duration
|
||||
// by the given increment for each attempt.
|
||||
func Incremental(initial, increment time.Duration) Algorithm {
|
||||
return func(attempt uint) time.Duration {
|
||||
return initial + (increment * time.Duration(attempt))
|
||||
}
|
||||
}
|
||||
|
||||
// Linear creates a Algorithm that linearly multiplies the factor
|
||||
// duration by the attempt number for each attempt.
|
||||
func Linear(factor time.Duration) Algorithm {
|
||||
return func(attempt uint) time.Duration {
|
||||
return factor * time.Duration(attempt)
|
||||
}
|
||||
}
|
||||
|
||||
// Exponential creates a Algorithm that multiplies the factor duration by
|
||||
// an exponentially increasing factor for each attempt, where the factor is
|
||||
// calculated as the given base raised to the attempt number.
|
||||
func Exponential(factor time.Duration, base float64) Algorithm {
|
||||
return func(attempt uint) time.Duration {
|
||||
return factor * time.Duration(math.Pow(base, float64(attempt)))
|
||||
}
|
||||
}
|
||||
|
||||
// BinaryExponential creates a Algorithm that multiplies the factor
|
||||
// duration by an exponentially increasing factor for each attempt, where the
|
||||
// factor is calculated as `2` raised to the attempt number (2^attempt).
|
||||
func BinaryExponential(factor time.Duration) Algorithm {
|
||||
return Exponential(factor, 2)
|
||||
}
|
||||
|
||||
// Fibonacci creates a Algorithm that multiplies the factor duration by
|
||||
// an increasing factor for each attempt, where the factor is the Nth number in
|
||||
// the Fibonacci sequence.
|
||||
func Fibonacci(factor time.Duration) Algorithm {
|
||||
return func(attempt uint) time.Duration {
|
||||
return factor * time.Duration(fibonacciNumber(attempt))
|
||||
}
|
||||
}
|
||||
|
||||
// fibonacciNumber calculates the Fibonacci sequence number for the given
|
||||
// sequence position.
|
||||
func fibonacciNumber(n uint) uint {
|
||||
if 0 == n {
|
||||
return 0
|
||||
} else if 1 == n {
|
||||
return 1
|
||||
} else {
|
||||
return fibonacciNumber(n-1) + fibonacciNumber(n-2)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
// Package backoff contains backoff algorithm helper functions.
|
||||
package backoff
|
|
@ -0,0 +1,68 @@
|
|||
package misc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type (
|
||||
handlerImpl struct {
|
||||
errorChan chan error
|
||||
ctx context.Context
|
||||
canceler context.CancelFunc
|
||||
waiter sync.WaitGroup
|
||||
}
|
||||
|
||||
// Handler is context handler with sync.WaitGroup and error notifier.
|
||||
Handler interface {
|
||||
// NotifyError is an error notifier method within a concurrency environment that avoids falling in deadlock.
|
||||
NotifyError(err error)
|
||||
// Error method is a Error channel for cancellation.
|
||||
Error() <-chan error
|
||||
// Done method is a Done channel for cancellation.
|
||||
Done() <-chan struct{}
|
||||
// GracefulWait is a graceful shutdown method while the dependents are safely released.
|
||||
// It acts like a "WaitGroup.Wait" method.
|
||||
GracefulWait()
|
||||
// IncreaseWait is an acquiring method for a dependent.
|
||||
// It acts like a "WaitGroup.Add(1)" method.
|
||||
IncreaseWait()
|
||||
// DecreaseWait is an release method for a dependent.
|
||||
// It acts like a "WaitGroup.Done" method.
|
||||
DecreaseWait()
|
||||
}
|
||||
)
|
||||
|
||||
// NewHandler returns an initialized Handler interface.
|
||||
func NewHandler(ctx context.Context) Handler {
|
||||
ctx, canceler := context.WithCancel(ctx)
|
||||
return &handlerImpl{
|
||||
ctx: ctx,
|
||||
canceler: canceler,
|
||||
errorChan: make(chan error, 5),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *handlerImpl) NotifyError(err error) { h.errorChan <- err }
|
||||
func (h *handlerImpl) Error() <-chan error { return h.errorChan }
|
||||
func (h *handlerImpl) Done() <-chan struct{} { return h.ctx.Done() }
|
||||
func (h *handlerImpl) GracefulWait() {
|
||||
if h.ctx.Err() == nil {
|
||||
h.canceler()
|
||||
}
|
||||
|
||||
h.waiter.Wait()
|
||||
close(h.errorChan)
|
||||
|
||||
for remainError := range h.errorChan {
|
||||
log.Println("remain errors ", remainError)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *handlerImpl) IncreaseWait() {
|
||||
h.waiter.Add(1)
|
||||
}
|
||||
func (h *handlerImpl) DecreaseWait() {
|
||||
h.waiter.Done()
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
// Package crypto contains encryption-decryption helper functions.
|
||||
package crypto
|
|
@ -0,0 +1,18 @@
|
|||
package crypto
|
||||
|
||||
// Pkcs5pad is a padding function that uses the PKCS5 method.
|
||||
func Pkcs5pad(data []byte, blocksize int) []byte {
|
||||
pad := blocksize - len(data)%blocksize
|
||||
b := make([]byte, pad, pad)
|
||||
for i := 0; i < pad; i++ {
|
||||
b[i] = uint8(pad)
|
||||
}
|
||||
return append(data, b...)
|
||||
}
|
||||
|
||||
// Pkcs5unpad is a stripping function that reverts the PKCS5 method.
|
||||
func Pkcs5unpad(data []byte) []byte {
|
||||
pad := int(data[len(data)-1])
|
||||
// FIXME: check that the padding bytes are all what we expect
|
||||
return data[:len(data)-pad]
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
package crypto
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"github.com/minio/sio"
|
||||
"io"
|
||||
)
|
||||
|
||||
/*
|
||||
NewSecretBox returns a SecretBox object with a provided key.
|
||||
|
||||
Here is the validation code written for Python:
|
||||
import os,binascii
|
||||
key=binascii.b2a_base64(os.urandom(32),newline=False)
|
||||
print(key.decode('us-ascii'))
|
||||
*/
|
||||
func NewSecretBox(key []byte) SecretBox {
|
||||
streamConfig := sio.Config{
|
||||
Key: key,
|
||||
Rand: rand.Reader,
|
||||
}
|
||||
var (
|
||||
encryptReader = func(src io.Reader) (io.Reader, error) {
|
||||
return sio.EncryptReader(src, streamConfig)
|
||||
}
|
||||
decryptReader = func(src io.Reader) (io.Reader, error) {
|
||||
return sio.DecryptReader(src, streamConfig)
|
||||
}
|
||||
encryptWriter = func(dst io.Writer) (io.WriteCloser, error) {
|
||||
return sio.EncryptWriter(dst, streamConfig)
|
||||
}
|
||||
decryptWriter = func(dst io.Writer) (io.WriteCloser, error) {
|
||||
return sio.DecryptWriter(dst, streamConfig)
|
||||
}
|
||||
)
|
||||
return &secretBoxImpl{
|
||||
encryptReaderGen: encryptReader,
|
||||
decryptReaderGen: decryptReader,
|
||||
encryptWriterGen: encryptWriter,
|
||||
decryptWriterGen: decryptWriter,
|
||||
}
|
||||
}
|
||||
|
||||
// SecretBox is an encryption and decryption provider.
|
||||
type SecretBox interface {
|
||||
// NewEncryptReader returns EncryptReader from given io.Reader.
|
||||
NewEncryptReader(io.Reader) (io.Reader, error)
|
||||
// NewDecryptReader returns DecryptReader from given io.Reader.
|
||||
NewDecryptReader(src io.Reader) (io.Reader, error)
|
||||
// NewEncryptWriter returns EncryptWriter from given io.Writer.
|
||||
NewEncryptWriter(dst io.Writer) (io.WriteCloser, error)
|
||||
// NewDecryptWriter returns DecryptWriter from given io.Writer.
|
||||
NewDecryptWriter(dst io.Writer) (io.WriteCloser, error)
|
||||
// EncryptedSize returns encrypted data size from given raw data size.
|
||||
EncryptedSize(size uint64) (uint64, error)
|
||||
// DecryptedSize returns decrypted data size from given raw data size.
|
||||
DecryptedSize(size uint64) (uint64, error)
|
||||
}
|
||||
|
||||
type secretBoxImpl struct {
|
||||
encryptReaderGen func(src io.Reader) (io.Reader, error)
|
||||
decryptReaderGen func(src io.Reader) (io.Reader, error)
|
||||
encryptWriterGen func(dst io.Writer) (io.WriteCloser, error)
|
||||
decryptWriterGen func(dst io.Writer) (io.WriteCloser, error)
|
||||
}
|
||||
|
||||
// 인터페이스가 실제 dto랑 호환되는가
|
||||
var _ SecretBox = (*secretBoxImpl)(nil)
|
||||
|
||||
func (x *secretBoxImpl) NewEncryptReader(r io.Reader) (io.Reader, error) {
|
||||
return x.encryptReaderGen(r)
|
||||
}
|
||||
func (x *secretBoxImpl) NewDecryptReader(r io.Reader) (io.Reader, error) {
|
||||
return x.decryptReaderGen(r)
|
||||
}
|
||||
func (x *secretBoxImpl) NewEncryptWriter(w io.Writer) (io.WriteCloser, error) {
|
||||
return x.encryptWriterGen(w)
|
||||
}
|
||||
func (x *secretBoxImpl) NewDecryptWriter(w io.Writer) (io.WriteCloser, error) {
|
||||
return x.decryptWriterGen(w)
|
||||
}
|
||||
func (x *secretBoxImpl) EncryptedSize(size uint64) (uint64, error) { return sio.EncryptedSize(size) }
|
||||
func (x *secretBoxImpl) DecryptedSize(size uint64) (uint64, error) { return sio.DecryptedSize(size) }
|
|
@ -0,0 +1,392 @@
|
|||
package crypto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"crypto/sha1"
|
||||
"crypto/subtle"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestShortEncryption1(t *testing.T) {
|
||||
key := make([]byte, 32)
|
||||
rand.Read(key)
|
||||
box := NewSecretBox(key)
|
||||
source := "h"
|
||||
hasher := sha1.New()
|
||||
_, err := strings.NewReader(source).WriteTo(hasher)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
formerHash := hasher.Sum(nil)
|
||||
t.Log("former hash ", hex.EncodeToString(formerHash))
|
||||
|
||||
encryptor, err := box.NewEncryptReader(strings.NewReader(source))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
trsnBuf := &bytes.Buffer{}
|
||||
_, err = trsnBuf.ReadFrom(encryptor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hasher.Reset()
|
||||
decrypted, err := box.NewDecryptWriter(hasher)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = trsnBuf.WriteTo(decrypted)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = decrypted.Close()
|
||||
latterHash := hasher.Sum(nil)
|
||||
t.Log("latter hash ", hex.EncodeToString(latterHash))
|
||||
if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
|
||||
t.Fatal("hash not matched!")
|
||||
} else {
|
||||
t.Log("Hash Match!")
|
||||
}
|
||||
}
|
||||
func TestShortEncryption2(t *testing.T) {
|
||||
key := make([]byte, 32)
|
||||
rand.Read(key)
|
||||
box := NewSecretBox(key)
|
||||
source := "h"
|
||||
hasher := sha1.New()
|
||||
_, err := strings.NewReader(source).WriteTo(hasher)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
formerHash := hasher.Sum(nil)
|
||||
t.Log("former hash ", hex.EncodeToString(formerHash))
|
||||
|
||||
encryptor, err := box.NewEncryptReader(strings.NewReader(source))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
trsnBuf := &bytes.Buffer{}
|
||||
_, err = trsnBuf.ReadFrom(encryptor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hasher.Reset()
|
||||
decrypted, err := box.NewDecryptReader(trsnBuf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = io.Copy(hasher, decrypted)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
latterHash := hasher.Sum(nil)
|
||||
t.Log("latter hash ", hex.EncodeToString(latterHash))
|
||||
if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
|
||||
t.Fatal("hash not matched!")
|
||||
} else {
|
||||
t.Log("Hash Match!")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShortEncryption3(t *testing.T) {
|
||||
key := make([]byte, 32)
|
||||
rand.Read(key)
|
||||
box := NewSecretBox(key)
|
||||
source := "h"
|
||||
hasher := sha1.New()
|
||||
_, err := strings.NewReader(source).WriteTo(hasher)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
formerHash := hasher.Sum(nil)
|
||||
t.Log("former hash ", hex.EncodeToString(formerHash))
|
||||
|
||||
trsnBuf := &bytes.Buffer{}
|
||||
encryptor, err := box.NewEncryptWriter(trsnBuf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = strings.NewReader(source).WriteTo(encryptor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = encryptor.Close()
|
||||
hasher.Reset()
|
||||
decrypted, err := box.NewDecryptReader(trsnBuf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = io.Copy(hasher, decrypted)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
latterHash := hasher.Sum(nil)
|
||||
t.Log("latter hash ", hex.EncodeToString(latterHash))
|
||||
if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
|
||||
t.Fatal("hash not matched!")
|
||||
} else {
|
||||
t.Log("Hash Match!")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShortEncryption4(t *testing.T) {
|
||||
key := make([]byte, 32)
|
||||
rand.Read(key)
|
||||
box := NewSecretBox(key)
|
||||
source := "h"
|
||||
hasher := sha1.New()
|
||||
_, err := strings.NewReader(source).WriteTo(hasher)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
formerHash := hasher.Sum(nil)
|
||||
t.Log("former hash ", hex.EncodeToString(formerHash))
|
||||
|
||||
trsnBuf := &bytes.Buffer{}
|
||||
encryptor, err := box.NewEncryptWriter(trsnBuf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = strings.NewReader(source).WriteTo(encryptor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = encryptor.Close()
|
||||
hasher.Reset()
|
||||
decrypted, err := box.NewDecryptWriter(hasher)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = trsnBuf.WriteTo(decrypted)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = decrypted.Close()
|
||||
latterHash := hasher.Sum(nil)
|
||||
t.Log("latter hash ", hex.EncodeToString(latterHash))
|
||||
if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
|
||||
t.Fatal("hash not matched!")
|
||||
} else {
|
||||
t.Log("Hash Match!")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLongEncryption1(t *testing.T) {
|
||||
key := make([]byte, 32)
|
||||
rand.Read(key)
|
||||
|
||||
buf := make([]byte, 1024*1024*5)
|
||||
rand.Read(buf)
|
||||
|
||||
box := NewSecretBox(key)
|
||||
hasher := sha1.New()
|
||||
_, err := hasher.Write(buf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
formerHash := hasher.Sum(nil)
|
||||
t.Log("former hash ", hex.EncodeToString(formerHash))
|
||||
|
||||
encryptor, err := box.NewEncryptReader(bytes.NewReader(buf))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
trsnBuf := &bytes.Buffer{}
|
||||
_, err = trsnBuf.ReadFrom(encryptor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hasher.Reset()
|
||||
decrypted, err := box.NewDecryptWriter(hasher)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = trsnBuf.WriteTo(decrypted)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = decrypted.Close()
|
||||
latterHash := hasher.Sum(nil)
|
||||
t.Log("latter hash ", hex.EncodeToString(latterHash))
|
||||
if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
|
||||
t.Fatal("hash not matched!")
|
||||
} else {
|
||||
t.Log("Hash Match!")
|
||||
}
|
||||
}
|
||||
func TestLongEncryption2(t *testing.T) {
|
||||
key := make([]byte, 32)
|
||||
rand.Read(key)
|
||||
box := NewSecretBox(key)
|
||||
|
||||
buf := make([]byte, 1024*1024*5)
|
||||
rand.Read(buf)
|
||||
|
||||
hasher := sha1.New()
|
||||
_, err := hasher.Write(buf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
formerHash := hasher.Sum(nil)
|
||||
t.Log("former hash ", hex.EncodeToString(formerHash))
|
||||
|
||||
encryptor, err := box.NewEncryptReader(bytes.NewReader(buf))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
trsnBuf := &bytes.Buffer{}
|
||||
_, err = trsnBuf.ReadFrom(encryptor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hasher.Reset()
|
||||
decrypted, err := box.NewDecryptReader(trsnBuf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = io.Copy(hasher, decrypted)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
latterHash := hasher.Sum(nil)
|
||||
t.Log("latter hash ", hex.EncodeToString(latterHash))
|
||||
if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
|
||||
t.Fatal("hash not matched!")
|
||||
} else {
|
||||
t.Log("Hash Match!")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLongEncryption3(t *testing.T) {
|
||||
key := make([]byte, 32)
|
||||
rand.Read(key)
|
||||
box := NewSecretBox(key)
|
||||
|
||||
buf := make([]byte, 1024*1024*5)
|
||||
rand.Read(buf)
|
||||
|
||||
hasher := sha1.New()
|
||||
_, err := hasher.Write(buf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
formerHash := hasher.Sum(nil)
|
||||
t.Log("former hash ", hex.EncodeToString(formerHash))
|
||||
|
||||
trsnBuf := &bytes.Buffer{}
|
||||
encryptor, err := box.NewEncryptWriter(trsnBuf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = encryptor.Write(buf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = encryptor.Close()
|
||||
hasher.Reset()
|
||||
decrypted, err := box.NewDecryptReader(trsnBuf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = io.Copy(hasher, decrypted)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
latterHash := hasher.Sum(nil)
|
||||
t.Log("latter hash ", hex.EncodeToString(latterHash))
|
||||
if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
|
||||
t.Fatal("hash not matched!")
|
||||
} else {
|
||||
t.Log("Hash Match!")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLongEncryption4(t *testing.T) {
|
||||
key := make([]byte, 32)
|
||||
rand.Read(key)
|
||||
box := NewSecretBox(key)
|
||||
|
||||
buf := make([]byte, 1024*1024*5)
|
||||
rand.Read(buf)
|
||||
|
||||
hasher := sha1.New()
|
||||
_, err := hasher.Write(buf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
formerHash := hasher.Sum(nil)
|
||||
t.Log("former hash ", hex.EncodeToString(formerHash))
|
||||
|
||||
trsnBuf := &bytes.Buffer{}
|
||||
encryptor, err := box.NewEncryptWriter(trsnBuf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = encryptor.Write(buf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = encryptor.Close()
|
||||
hasher.Reset()
|
||||
decrypted, err := box.NewDecryptWriter(hasher)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = trsnBuf.WriteTo(decrypted)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = decrypted.Close()
|
||||
latterHash := hasher.Sum(nil)
|
||||
t.Log("latter hash ", hex.EncodeToString(latterHash))
|
||||
if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
|
||||
t.Fatal("hash not matched!")
|
||||
} else {
|
||||
t.Log("Hash Match!")
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
//func TestLongEncryptionDescription(t *testing.T) {
|
||||
// var key [32]byte
|
||||
// rand.Read(key[:])
|
||||
//
|
||||
// buf := make([]byte, 1024*1024*5)
|
||||
// rand.Read(buf)
|
||||
// hasher := sha1.New()
|
||||
// hasher.Write(buf)
|
||||
// formerHash := hasher.Sum(nil)
|
||||
// t.Log("former hash ", hex.EncodeToString(formerHash))
|
||||
//
|
||||
// var encryptBuf bytes.Buffer
|
||||
// encryptor, err := NewEncryptor(&encryptBuf, &key)
|
||||
// if err != nil {
|
||||
// t.Fatal("cannot create crypto", err)
|
||||
// }
|
||||
// _, err = encryptor.Write(buf)
|
||||
// if err != nil {
|
||||
// t.Fatal("cannot create encryptorGenerator", err)
|
||||
// }
|
||||
// err = encryptor.Close()
|
||||
//
|
||||
// decryptor, err := NewDecryptor(&encryptBuf, &key)
|
||||
// if err != nil {
|
||||
// t.Fatal("cannot create decryptorGenerator", err)
|
||||
// }
|
||||
// decrypted, err := ioutil.ReadAll(decryptor)
|
||||
// if err != nil {
|
||||
// t.Fatal("error occurred while reading ", err)
|
||||
// }
|
||||
// hasher.Reset()
|
||||
// hasher.Write(decrypted)
|
||||
// latterHash := hasher.Sum(nil)
|
||||
// t.Log("former hash ", hex.EncodeToString(latterHash))
|
||||
// if subtle.ConstantTimeCompare(formerHash, latterHash) != 1 {
|
||||
// t.Fatal("hash not matched!")
|
||||
// } else {
|
||||
// t.Log("Hash Match!")
|
||||
// }
|
||||
//}
|
|
@ -0,0 +1,120 @@
|
|||
package crypto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/cipher"
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
// ErrShortCiphertext is an error that has text too short.
|
||||
var ErrShortCiphertext = errors.New("input too short to be valid ciphertext")
|
||||
|
||||
/*
|
||||
NewCryptoWriter returns block cipher writer.
|
||||
from https://github.com/acasajus/dkeyczar/blob/master/streams.go
|
||||
*/
|
||||
func NewCryptoWriter(bm cipher.BlockMode, sink io.WriteCloser) io.WriteCloser {
|
||||
return &cryptoWriter{
|
||||
bm: bm,
|
||||
sink: sink,
|
||||
buffer: bytes.NewBuffer(nil),
|
||||
}
|
||||
}
|
||||
|
||||
type cryptoWriter struct {
|
||||
bm cipher.BlockMode
|
||||
buffer *bytes.Buffer
|
||||
sink io.WriteCloser
|
||||
count int
|
||||
}
|
||||
|
||||
func (c *cryptoWriter) Write(data []byte) (int, error) {
|
||||
if _, err := c.buffer.Write(data); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
bL := c.buffer.Len() - c.buffer.Len()%c.bm.BlockSize()
|
||||
tmp := c.buffer.Next(bL)
|
||||
c.bm.CryptBlocks(tmp, tmp)
|
||||
wL := 0
|
||||
for wL < len(tmp) {
|
||||
n, err := c.sink.Write(tmp[wL:])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
wL += n
|
||||
}
|
||||
c.count += wL
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
func (c *cryptoWriter) Close() error {
|
||||
tmp := Pkcs5pad(c.buffer.Next(c.buffer.Len()), c.bm.BlockSize())
|
||||
c.bm.CryptBlocks(tmp, tmp)
|
||||
wL := 0
|
||||
for wL < len(tmp) {
|
||||
n, err := c.sink.Write(tmp[wL:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
wL += n
|
||||
}
|
||||
c.count += wL
|
||||
return c.sink.Close()
|
||||
}
|
||||
|
||||
func NewCryptoReader(bm cipher.BlockMode, source io.ReadCloser) io.ReadCloser {
|
||||
return &cryptoReader{
|
||||
bm: bm,
|
||||
source: source,
|
||||
outBuf: bytes.NewBuffer(nil),
|
||||
inBuf: bytes.NewBuffer(nil),
|
||||
eof: false,
|
||||
}
|
||||
}
|
||||
|
||||
type cryptoReader struct {
|
||||
bm cipher.BlockMode
|
||||
outBuf *bytes.Buffer
|
||||
inBuf *bytes.Buffer
|
||||
source io.ReadCloser
|
||||
eof bool
|
||||
}
|
||||
|
||||
func (cr *cryptoReader) Read(data []byte) (int, error) {
|
||||
missing := len(data) - cr.outBuf.Len()
|
||||
for !cr.eof && missing > 0 {
|
||||
toRead := missing + cr.bm.BlockSize() + 1 //Always go beyond the required data to be able to unpad when eof'ed
|
||||
if off := toRead % cr.bm.BlockSize(); off > 0 {
|
||||
toRead += cr.bm.BlockSize() - off //Make sure we read in multiples of blocksize
|
||||
}
|
||||
cr.inBuf.Grow(toRead)
|
||||
n, err := io.CopyN(cr.inBuf, cr.source, int64(toRead))
|
||||
if err == io.EOF {
|
||||
cr.eof = true
|
||||
} else if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
readBytes := int(n)
|
||||
if readBytes%cr.bm.BlockSize() > 0 && cr.eof {
|
||||
return 0, ErrShortCiphertext
|
||||
}
|
||||
bytesToDec := readBytes - readBytes%cr.bm.BlockSize()
|
||||
tmpdata := cr.inBuf.Next(bytesToDec)
|
||||
cr.bm.CryptBlocks(tmpdata, tmpdata)
|
||||
if _, err := cr.outBuf.Write(tmpdata); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if cr.eof {
|
||||
pad := cr.outBuf.Bytes()[cr.outBuf.Len()-1]
|
||||
cr.outBuf.Truncate(cr.outBuf.Len() - int(pad))
|
||||
}
|
||||
missing = len(data) - cr.outBuf.Len()
|
||||
}
|
||||
return cr.outBuf.Read(data)
|
||||
}
|
||||
|
||||
func (cr *cryptoReader) Close() error {
|
||||
cr.eof = true
|
||||
return cr.source.Close()
|
||||
}
|
|
@ -0,0 +1,111 @@
|
|||
package dbg
|
||||
|
||||
import "bytes"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/text/width"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// HexDump returns hexadecimal data dump data.
|
||||
func HexDump(by []byte) string {
|
||||
n := len(by)
|
||||
rowcount := 0
|
||||
stop := (n / 16) * 16
|
||||
k := 0
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
for i := 0; i <= stop; i += 16 {
|
||||
k++
|
||||
if i+16 < n {
|
||||
rowcount = 16
|
||||
} else {
|
||||
rowcount = min(k*16, n) % 16
|
||||
}
|
||||
|
||||
fmt.Fprintf(buf, "%08x ", i)
|
||||
for j := 0; j < rowcount; j++ {
|
||||
if j%8 == 0 {
|
||||
fmt.Fprintf(buf, " %02x ", by[i+j])
|
||||
} else {
|
||||
fmt.Fprintf(buf, "%02x ", by[i+j])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for j := rowcount; j < 16; j++ {
|
||||
if j%8 == 0 {
|
||||
fmt.Fprintf(buf, " ")
|
||||
} else {
|
||||
fmt.Fprintf(buf, " ")
|
||||
}
|
||||
}
|
||||
buf.WriteRune('|')
|
||||
viewString(by[i:(i+rowcount)], buf)
|
||||
buf.WriteRune('|')
|
||||
buf.WriteRune('\n')
|
||||
buf.WriteRune('\r')
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// GuessUnicodeWidth returns the size of bytes for a single rune.
|
||||
func GuessUnicodeWidth(char rune) (realSize int) {
|
||||
prop := width.LookupRune(char)
|
||||
switch prop.Kind() {
|
||||
case width.EastAsianFullwidth:
|
||||
fallthrough
|
||||
case width.EastAsianWide:
|
||||
realSize = 2
|
||||
case width.EastAsianHalfwidth:
|
||||
fallthrough
|
||||
case width.EastAsianNarrow:
|
||||
realSize = 2
|
||||
case width.EastAsianAmbiguous:
|
||||
fallthrough
|
||||
case width.Neutral:
|
||||
fallthrough
|
||||
default:
|
||||
realSize = 1
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func viewString(b []byte, buf *bytes.Buffer) {
|
||||
for {
|
||||
if r, size := utf8.DecodeRune(b); size == 0 {
|
||||
return
|
||||
} else if r == utf8.RuneError {
|
||||
for i := 0; i < size; i++ {
|
||||
buf.WriteRune('_')
|
||||
}
|
||||
b = b[size:]
|
||||
} else if r < 32 {
|
||||
for i := 0; i < size; i++ {
|
||||
buf.WriteRune('.')
|
||||
}
|
||||
b = b[size:]
|
||||
} else {
|
||||
buf.WriteRune(r)
|
||||
pad := max(0, size-GuessUnicodeWidth(r))
|
||||
for i := 0; i < pad; i++ {
|
||||
buf.WriteRune('.')
|
||||
}
|
||||
b = b[size:]
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
// Package dbg contains debugging helper functions.
|
||||
package dbg
|
|
@ -0,0 +1,10 @@
|
|||
package dttm
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
//BetweenTwoDates is a function that checks if a given date is between a start date and an end date.
|
||||
func BetweenTwoDates(target, start, end time.Time) bool {
|
||||
return (start.Before(target) || start.Equal(target)) && end.After(target)
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package dttm
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
// SeoulTZ variable exposes "Asia/Seoul" timezone.
|
||||
SeoulTZ, _ = time.LoadLocation("Asia/Seoul")
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
// Package dttm contains date-time and locale-related data structures.
|
||||
package dttm
|
|
@ -0,0 +1,25 @@
|
|||
module amuz.es/src/go/misc
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/ericlagergren/decimal v0.0.0-20211103172832-aca2edc11f73
|
||||
github.com/jmoiron/sqlx v1.3.4
|
||||
github.com/json-iterator/go v1.1.12
|
||||
github.com/minio/sio v0.3.0
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/valyala/fasthttp v1.34.0
|
||||
go.uber.org/multierr v1.8.0
|
||||
golang.org/x/text v0.3.7
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/andybalholm/brotli v1.0.4 // indirect
|
||||
github.com/klauspost/compress v1.15.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
|
||||
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
|
||||
)
|
|
@ -0,0 +1,121 @@
|
|||
package han
|
||||
|
||||
import (
|
||||
"github.com/valyala/fasthttp"
|
||||
"go.uber.org/multierr"
|
||||
"golang.org/x/text/encoding/korean"
|
||||
"golang.org/x/text/transform"
|
||||
"net/url"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
const (
|
||||
hexConstUpper = "0123456789ABCDEF"
|
||||
)
|
||||
|
||||
// ConvertFormEUCKRFasthttp is translate method from euc-kr encoded fasthttp.Args to utf-8 encoded fasthttp.Args.
|
||||
func ConvertFormEUCKRFasthttp(rawParsed *fasthttp.Args) (parsed *fasthttp.Args, err error) {
|
||||
var (
|
||||
//rawKey, rawValue string
|
||||
decoder = korean.EUCKR.NewDecoder()
|
||||
)
|
||||
|
||||
parsed = &fasthttp.Args{}
|
||||
rawParsed.VisitAll(func(k, v []byte) {
|
||||
if rawKey, decodeErr := decoder.Bytes(k); decodeErr != nil {
|
||||
err = multierr.Append(err, decodeErr)
|
||||
} else if rawValue, decodeErr := decoder.Bytes(v); decodeErr != nil {
|
||||
err = multierr.Append(err, decodeErr)
|
||||
} else {
|
||||
parsed.SetBytesKV(rawKey, rawValue)
|
||||
}
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ConvertFormEUCKR is translate method from euc-kr encoded url.Values to utf-8 encoded url.Values.
|
||||
func ConvertFormEUCKR(rawParsed url.Values) (parsed url.Values, err error) {
|
||||
var (
|
||||
rawKey, rawValue string
|
||||
decoder = korean.EUCKR.NewDecoder()
|
||||
)
|
||||
|
||||
parsed = make(url.Values, len(rawParsed))
|
||||
for k, v := range rawParsed {
|
||||
rawKey, err = decoder.String(k)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
valueList := make([]string, 0, len(v))
|
||||
for _, vi := range v {
|
||||
rawValue, err = decoder.String(vi)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
valueList = append(valueList, rawValue)
|
||||
}
|
||||
parsed[rawKey] = valueList
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ConvertToEUCKRUrlEncoded is translate method from utf-8 encoded string to euc-kr encoded string.
|
||||
func ConvertToEUCKRUrlEncoded(str string) (converted string, err error) {
|
||||
var (
|
||||
buf strings.Builder
|
||||
encoder = korean.EUCKR.NewEncoder()
|
||||
size int
|
||||
)
|
||||
for _, r := range str {
|
||||
switch {
|
||||
case unicode.IsNumber(r) ||
|
||||
unicode.Is(unicode.Latin, r) ||
|
||||
r == '-' ||
|
||||
r == '_' ||
|
||||
r == '.':
|
||||
buf.WriteRune(r)
|
||||
case unicode.IsSpace(r):
|
||||
buf.WriteByte('+')
|
||||
case r == '$' ||
|
||||
r == '&' ||
|
||||
r == '+' ||
|
||||
r == ',' ||
|
||||
r == '/' ||
|
||||
r == ':' ||
|
||||
r == ';' ||
|
||||
r == '=' ||
|
||||
r == '?' ||
|
||||
r == '@':
|
||||
buf.WriteByte('%')
|
||||
buf.WriteByte(hexConstUpper[byte(r)>>4])
|
||||
buf.WriteByte(hexConstUpper[byte(r)&15])
|
||||
default:
|
||||
converted, size, err = transform.String(encoder, string(r))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
buf.WriteByte('%')
|
||||
buf.WriteByte(hexConstUpper[(converted[0]>>4)&0xf])
|
||||
buf.WriteByte(hexConstUpper[converted[0]&0xf])
|
||||
if size > 1 {
|
||||
buf.WriteByte('%')
|
||||
buf.WriteByte(hexConstUpper[(converted[1]>>4)&0xf])
|
||||
buf.WriteByte(hexConstUpper[converted[1]&0xf])
|
||||
}
|
||||
}
|
||||
}
|
||||
converted = buf.String()
|
||||
return
|
||||
}
|
||||
|
||||
// ConvertFromEUCKRUrlEncoded is translate method from euc-kr encoded string to utf-8 encoded string.
|
||||
func ConvertFromEUCKRUrlEncoded(str string) (converted string, err error) {
|
||||
dst, err := url.QueryUnescape(str)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
converted, _, err = transform.String(korean.EUCKR.NewDecoder(), dst)
|
||||
return
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package han
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConvertToEUCKRUrlEncoded(t *testing.T) {
|
||||
|
||||
// result string get from http://code.cside.com/3rdpage/us/url/converter.html
|
||||
cases := []struct {
|
||||
in string
|
||||
out string
|
||||
}{
|
||||
{
|
||||
"가나다 abc ABC 123 ~!@#$%^&*()_+`-=[]{}|;':,./<>?",
|
||||
"%B0%A1%B3%AA%B4%D9+abc+ABC+123+%7E%21%40%23%24%25%5E%26%2A%28%29_%2B%60-%3D%5B%5D%7B%7D%7C%3B%27%3A%2C.%2F%3C%3E%3F",
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
result, err := ConvertToEUCKRUrlEncoded(c.in)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if result != c.out {
|
||||
t.Errorf("src=%s\nresult1=%s\nresult2=%s\n", c.in, c.out, result)
|
||||
} else {
|
||||
t.Log("OK")
|
||||
}
|
||||
result2, err := ConvertFromEUCKRUrlEncoded(result)
|
||||
if err != nil {
|
||||
t.Error("decode err", err)
|
||||
} else {
|
||||
if result2 != c.in {
|
||||
t.Error("decode err2", result2, c.in)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
package han
|
||||
|
||||
import (
|
||||
"amuz.es/src/go/misc/strutil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
koreanPluralizeSubunit = 10
|
||||
koreanPluralizeUnit = 10000
|
||||
zeroChr = '0'
|
||||
)
|
||||
|
||||
var (
|
||||
koreanPluralizeUnitLabels = []string{"", "만", "억", "조"}
|
||||
koreanPluralizeSubunitLabels = []string{"", "십", "백", "천"}
|
||||
)
|
||||
|
||||
func transKoreanPluralizeSubunit(number int) (out string) {
|
||||
if number == 0 {
|
||||
return "0"
|
||||
}
|
||||
var (
|
||||
buf strings.Builder
|
||||
digit int
|
||||
)
|
||||
for radix := 0; number > 0; radix++ {
|
||||
digit, number = number%koreanPluralizeSubunit, number/koreanPluralizeSubunit
|
||||
if digit > 0 {
|
||||
buf.WriteString(koreanPluralizeSubunitLabels[radix%len(koreanPluralizeSubunitLabels)])
|
||||
}
|
||||
if digit > 1 || (digit == 1 && radix == 0) {
|
||||
buf.WriteByte(zeroChr + byte(digit))
|
||||
}
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func transKoreanPluralizeUnit(number int, subunitmapper func(int) string, omitLastOne bool) (out string) {
|
||||
if number == 0 {
|
||||
return "0"
|
||||
}
|
||||
var (
|
||||
buf strings.Builder
|
||||
digit, lastDigit int
|
||||
)
|
||||
for radix := 0; number > 0; radix++ {
|
||||
digit = number % koreanPluralizeUnit
|
||||
number = number / koreanPluralizeUnit
|
||||
lastDigit = digit % koreanPluralizeSubunit
|
||||
if digit > 0 {
|
||||
buf.WriteString(koreanPluralizeUnitLabels[radix%len(koreanPluralizeUnitLabels) |