小编icz*_*cza的帖子

Go中的String的MD5摘要与Java的不同

我正在用Java创建MD5摘要,这是计算输入字符串的4字节十六进制哈希所需要的.以下是Java中的代码:

public static String hashString(String s) {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(s.getBytes("US-ASCII"));
            byte[] output = new byte[digest.length / 4];
            for (int i = 0; i < output.length; i++) {
                for (int j = 0; j < digest.length; j += 4) {
                    System.out.print(digest[j]);
                    output[i] ^= digest[i + j];
                }
            }
            return getHexString(output);
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
           return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我想在Golang中使用相同的代码,但是,MD5输出与我在Java中获得的输出不同.以下是Go中的代码:

func hashString(s string) string {
    md := md5.New()
    md.Write([]byte(s))
    data := …
Run Code Online (Sandbox Code Playgroud)

java string hash md5 go

-3
推荐指数
1
解决办法
389
查看次数

使用空格解码base64

我有一个base64编码的字符串,我试图用go解密.该字符串包含应忽略的空格.我正在尝试的示例代码:

s := "eyJ0aHJlZURTU2VydmVyVHJhbnNJRCI6IjEzZmU3MWQ0LWQxMGQtNDIyMC1hMjE2LTIwMDZkMWRkNGNiOCIsImFjc1RyY++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++W5zSUQiOiJkN2M0NWY5OS05NDc4LTQ0YTYtYjFmMi0xMDAwMDAwMDMzNjYiLCJtZXNzYWdlVHlwZSI6IkNSZXEiLCJtZXNzYWdlVmVyc2lvbiI6IjIuMS4wIiwiY2hhbGxlbmdlV2luZG93U2l6ZSI6IjAyIn0%3D"

out, err := base64.URLEncoding.DecodeString(s)
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(string(out))
Run Code Online (Sandbox Code Playgroud)

此代码返回:

输入字节93处的非法base64数据


更改字符串填充后,使用StdEncoding而不是URLEncoding:

s= strings.Replace(s, "%3D", "=", -1)
out, err := base64.StdEncoding.DecodeString(s)
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println(string(out))
Run Code Online (Sandbox Code Playgroud)

输出将是:

{ "threeDSServerTransID": "13fe71d4-D10D-4220-a216-2006d1dd4cb8","acsTrc NSID ":" d7c45f99- 9478-44a6-b1f2-100000003366" , "为messageType": "CREQ", "messageVersion": "2.1.0", "challengeWindowSize": "02"}


如何正确解密字符串?

string base64 go url-encoding

-3
推荐指数
1
解决办法
1008
查看次数

Goroutine 概念:直接调用 vs 使用函数字面量调用

以下两个代码之间的基本区别是什么?不知何故,在第二个示例demo_process2()中从未被调用,但在第一个示例中工作正常。

    1.
go func() {
    Must(demo_process1())
}()

demo_process2()
Run Code Online (Sandbox Code Playgroud)
    2.
go Must(demo_process1())
demo_process2()
Run Code Online (Sandbox Code Playgroud)

其中Must()

func Must(err error) {
    if err != nil {
        panic(err)
    }
}
Run Code Online (Sandbox Code Playgroud)

concurrency go goroutine

-3
推荐指数
1
解决办法
119
查看次数

如何将数据从一种类型复制到具有相同结构的另一种类型?

在下面的代码中:

type ProductEntity struct {
    ID          int     `json:"id"`
    Name        string  `json:"name"`
    Description string  `json:"description"`
    Price       float32 `json:"price"`
    SKU         string  `json:"sku"`
    CreatedOn   string  `json:"-"`
    UpdatedOn   string  `json:"-"`
    DeletedOn   string  `json:"-"`
}

type ProductEntityList []*ProductEntity

type PostRequestModel struct {
    ID          int     `json:"id"`
    Name        string  `json:"name"`
    Description string  `json:"description"`
    Price       float32 `json:"price"`
    SKU         string  `json:"sku"`
    CreatedOn   string  `json:"-"`
    UpdatedOn   string  `json:"-"`
    DeletedOn   string  `json:"-"`
}

type RequestBody []*PostRequestModel

func convertModelToEntity(modelList RequestBody) ProductEntityList {

    // return entity.ProductEntityList(modelList) // type conversion error 

}
Run Code Online (Sandbox Code Playgroud)

如何将数据从一种类型复制到另一种具有相同结构的类型?因为RequestBody …

struct pointers go slice

-3
推荐指数
1
解决办法
54
查看次数

为什么在Go中使用:= with fmt.Scanf总是返回1?

我为什么用:

var n int
a, _ := fmt.Scanf("%d",&n)
Run Code Online (Sandbox Code Playgroud)

然后a == 1,n通过输入改变了它的值.

parsing scanf go

-5
推荐指数
1
解决办法
142
查看次数

为什么Go和Python在分割大数字时会返回不同的结果?

// In golang    
x, y := big.NewFloat(26959535291011309493156476344723991336010898738574164086137773096960), big.NewFloat(14484.162361)  
z := new(big.Float).Quo(x, y)
fmt.Println(fmt.Sprintf("%f", z))
Run Code Online (Sandbox Code Playgroud)

输出:1861311315012765262390495455137379355146730679910059382988079104.000000

// In python
v1 = 26959535291011309493156476344723991336010898738574164086137773096960000000
v2= 14484162361
print v1/v2
Run Code Online (Sandbox Code Playgroud)

输出:1861311315012765306929610463010191006516769515973403833769533170

python numbers division go

-5
推荐指数
1
解决办法
126
查看次数

同步一次实现

我在Go 1.12中有一个关于sync.Once()的问题。源代码如下:

// Because no call to Do returns until the one call to f returns, if f causes
// Do to be called, it will deadlock.

func (o *Once) Do(f func()) {
    if atomic.LoadUint32(&o.done) == 1 {
        return
    }
    // Slow-path.
    o.m.Lock()
    defer o.m.Unlock()
    if o.done == 0 {
        defer atomic.StoreUint32(&o.done, 1)
        f()
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么不只使用一个uint32变量,然后对该变量执行CAS。它似乎更有效,并且不会导致死锁。

代码如下:

type Once uint32
func (o *Once) Do(f func()) {
    if atomic.CompareAndSwapUint32((*uint32)(o), 0, 1) {
        f()
    }
}
Run Code Online (Sandbox Code Playgroud)

synchronization go

-5
推荐指数
1
解决办法
102
查看次数