密钥派生函数 @param Z @param klen 生成klen字节数长度的密钥 @return
(byte[] Z, int klen)
| 157 | * @return |
| 158 | */ |
| 159 | private static byte[] KDF(byte[] Z, int klen) { |
| 160 | int ct = 1; |
| 161 | int end = (int) Math.ceil(klen * 1.0 / 32); |
| 162 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 163 | try { |
| 164 | for (int i = 1; i < end; i++) { |
| 165 | baos.write(sm3hash(Z, SM3.toByteArray(ct))); |
| 166 | ct++; |
| 167 | } |
| 168 | byte[] last = sm3hash(Z, SM3.toByteArray(ct)); |
| 169 | if (klen % 32 == 0) { |
| 170 | baos.write(last); |
| 171 | } else { |
| 172 | baos.write(last, 0, klen % 32); |
| 173 | } |
| 174 | return baos.toByteArray(); |
| 175 | } catch (Exception e) { |
| 176 | e.printStackTrace(); |
| 177 | } |
| 178 | return null; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * 判断字节数组是否全0 |
no test coverage detected