Skip to content

Instantly share code, notes, and snippets.

View xieyuschen's full-sized avatar
☺️
Keep Loving, Keep Learning :)

Griffin xieyuschen

☺️
Keep Loving, Keep Learning :)
View GitHub Profile
@xieyuschen
xieyuschen / Rsa_using.md
Last active December 10, 2020 09:05
Encrypt and decrypt by ras pkcs1 by key-pair in pem files

Encrypt and decrypt by ras pkcs1 by key-pair in pem files

Generate Key-Pair randomly

Thanks to blog here, I finally understood how rsa encrypt and decrypt.

Generate public-private key pair

//copy from https://gist.github.com/miguelmota/3ea9286bd1d3c2a985b67cac4ba2130a 
//call GenerateKeyPair() can generate public-private key pem file for you
func GenerateKeyPair(){
@xieyuschen
xieyuschen / Unknown_encryption.md
Created December 10, 2020 17:32
Unknown type of public key

How to specify which way the key pair specified?

Here I record how to generate a key-pair to encrypt and decrypt. So I'm sure it use rsa pkcs1v15 to encrypt,and I can do encrypt and decrypt directly as the post above.
But when it comes to a given public-private key pair,thing seems unnormal:

Problem I encoutered:

When I encountered key pair in thoes files(pay attention that this is base64 encoding):

  • public.pem:
-----BEGIN PUBLIC KEY-----
@xieyuschen
xieyuschen / encounter_cors.md
Last active December 28, 2020 13:32
Cannot make a request to certain api for CORS

Cross Origin Resource Share

之前遇到的问题

之前的跨域问题很意外,hmm,我对这个东西还没有什么概念。情况是这样的,首先我写了几个api,然后监听8080端口。使用nginx进行代理,将对80访问的内容和对432访问的内容全部转发到127.0.0.1:8080去。然后前端在发请求的时候就遇到了跨域的问题。 解决方案:在nginx里面加一个同意跨域的设置 Access-Control-Allow-Origin:*。即可解决。

简单的提几个问题出来:

  • 跨域请求被阻塞发生在什么时候? 是nginx完成的阻塞,还是程序本身的阻塞

回答 阻塞由浏览器来完成,即正常发送请求,后端正常给予响应,但响应结果被浏览器拦截

@xieyuschen
xieyuschen / Nginx_cors.md
Last active December 28, 2020 13:54
How nginx deal with a CORS request

Nginx处理跨域请求

在过去处理跨域问题上,只要在nginx中加入如下内容,就可以解决跨域的问题:

add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

那么根据上篇文章,使用一个OPTIONS的方法就可以获取到一些跨域的内容, 然后浏览器经过预处理之后,就可以完成跨域的操作。这个经过实践应该来说是没有问题的,但写这个贴子希望可以稍微深入一点了解这个请求的内容是如何被nginx加上去的。

@xieyuschen
xieyuschen / download_file.md
Last active March 20, 2024 15:19
Gin: download file with an simple api

Gin: download file with an simple api

func DownloadLicense(ctx *gin.Context) {
	content:="Download file here happliy"
	fileName := "hello.txt"
	ctx.Header("Content-Disposition", "attachment; filename="+fileName)
	ctx.Header("Content-Type", "application/text/plain")
	ctx.Header("Accept-Length", fmt.Sprintf("%d", len(content)))
	ctx.Writer.Write([]byte(content))
	ctx.JSON(http.StatusOK, gin.H{
@xieyuschen
xieyuschen / incrementally_update.md
Created January 23, 2021 09:49
Incrementally Update Struct Golang

Incrementally Update Struct Golang

增量删除是很重要的,比如说在update的方法里面会出现这样的情况。

type demoStruct struct {
	Replaced string
	Remain   string
}
func main() {
	oldInfo := demoStruct{Replaced: "You can see me no more", Remain: "I still here"}
	fmt.Println(oldInfo.Replaced," ",oldInfo.Remain)
@xieyuschen
xieyuschen / ls_print.hs
Created April 28, 2023 04:30
execute ls and print the results by Haskell
{-# LANGUAGE BlockArguments #-}
import System.Process
import Data.Text.IO (hGetLine)
import GHC.IO.Handle (isEOF, hGetLine)
import GHC.IO.Handle.Types (Handle)
import Data.Type.Equality (outer)
main :: IO ()
main = do
(_, out , _, _) <- createProcess (proc "ls" []){ std_out = CreatePipe }
@xieyuschen
xieyuschen / why_monad_transformer.md
Created June 6, 2023 10:53
Haskell: why we need a monad transoformer?

Haskell: why we need a monad transoformer?

When learning the haskell monad transformer(would call it monadt in the left content), a problem in my mind is why monadT? Because from a simple example view, we could make a pale of monad together to do what we want we want to do.

Technially, it's correct, the monadT is just a wrapper. For example, those two cases are equal.

ioOp :: IO String
ioOp = getLine

maybeOp :: Maybe String -&gt; Maybe String
@xieyuschen
xieyuschen / difference_return_lift.md
Created June 6, 2023 11:31
Haskell: difference between return/pure and lift

Haskell: difference between return/pure and lift

In the following code snippet, return/pure cannot work as it constructs StateT s IO (IO ()), not StateT s IO ().

return/pure will construct the value based on the currect monad in context, which is StateT s IO. lift will upgrade the current monad(IO) to the new monad through the transformer.

printState :: Show s => StateT s IO ()
printState = do
 state &lt;- get
@xieyuschen
xieyuschen / discard_io.md
Created June 16, 2023 02:52
IO.Discard vs /Dev/Null file in Golang

IO.Discard Versus Writing /Dev/Null

Both of them could write down nothing, however, there is still a effieciency difference. Look at the bench:

package base

import (
	"io"
	"os"
	"testing"