package tests import ( "apigo.cloud/git/apigo/gojs" _ "apigo.cloud/git/apigo/plugins/crypto" "testing" ) func TestHash(t *testing.T) { r, err, _ := gojs.Run(` if(crypto.md5('hello 123') != '1bb45824de71b2f6476d800f427cb2ab') throw new Error('md5 error') if(crypto.sm3('hello 123') != '4e1f46e2350250d20df71c360852751c8aed29753e6058969602cea8639f7737') throw new Error('sm3 error') if(crypto.sha256(new Uint8Array([1,2,3,0,254])) != '39a55ea4bd5dd2fd3818501a1fff9bde99573465a2a5b82c9731f8efd2f0c814') throw new Error('sha256 error') return true `, nil, nil) Test(t, "Hash", r == true && err == nil, err) } func TestAes(t *testing.T) { r, err, _ := gojs.Run(` key = "?GQ$0K0GgLdO=f+~L68PLm$uhKr4'=tV" iv = new Uint8Array([86,70,115,55,64,115,75,54,49,99,106,94,102,63,72,90]) s1 = crypto.aes.encryptBytes('hello 123', key, iv) if( crypto.urlBase64.encodeBytes(s1) != 't2fsWsWbCDjMBqV_5WLgfQ==') throw new Error('urlBase64 encode error') s2 = crypto.aes.decrypt(crypto.hex.encodeBytes(s1), crypto.hex.encodeBytes(key), crypto.hex.encodeBytes(iv)) if( s2 != 'hello 123') throw new Error('aes decrypt') return true `, nil, nil) Test(t, "AES", r == true && err == nil, err) } func TestSM4(t *testing.T) { r, err, _ := gojs.Run(` key = "?GQ$0K0GgLdO=f+~L68PLm$uhKr4'=tV" iv = new Uint8Array([86,70,115,55,64,115,75,54,49,99,106,94,102,63,72,90]) s1 = crypto.sm4.encryptBytes('hello 123', key, iv) s2 = crypto.sm4.decrypt(crypto.hex.encodeBytes(s1), crypto.hex.encodeBytes(key), crypto.hex.encodeBytes(iv)) if( s2 != 'hello 123') throw new Error('sm4 decrypt error') return true `, nil, nil) Test(t, "SM4", r == true && err == nil, err) } func TestECDSA(t *testing.T) { r, err, _ := gojs.Run(` [priKey, pubKey] = crypto.ecdsa.genKey() s1 = crypto.ecdsa.sign('hello 123', priKey) if(!crypto.ecdsa.verify('hello 123', s1, pubKey)) throw new Error('') s2 = crypto.ecdsa.encrypt('hello 123', pubKey) s3 = crypto.ecdsa.decrypt(s2, priKey) if( s3 != 'hello 123') throw new Error('ecdsa decrypt error') return true `, nil, nil) Test(t, "ECDSA", r == true && err == nil, err) } func TestSM2(t *testing.T) { r, err, _ := gojs.Run(` [priKey, pubKey] = crypto.sm2.genKey() s1 = crypto.sm2.sign('hello 123', priKey) if(!crypto.sm2.verify('hello 123', s1, pubKey)) throw new Error('') s2 = crypto.sm2.encrypt('hello 123', pubKey) s3 = crypto.sm2.decrypt(s2, priKey) if( s3 != 'hello 123') throw new Error('sm2 decrypt error') return true `, nil, nil) Test(t, "SM2", r == true && err == nil, err) }